views:

104

answers:

3

When I execute this query:

Dim Var
Var = ("select max(Autonumber) from tblAutonumber")
DoCmd.RunSQL (Var)

I am getting the value of var as "select max(Autonumber) from tblAutonumber" instead of the maximum value I am looking for.

Code:

Private Sub btnSubmit_Enter()
DoCmd.RunSQL ("insert into tblAutonumber (Dummy)values ('DummyValue')")

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strMaxNum As String
Dim strSQL As String

strSQL = "select max(Autonumber) as maxNum from tblAutonumber"
Set db = CurrentDb
Set rst = db.OpenRecordset(strSQL)

 rst.MoveFirst
 strMaxNum = rst!maxNum
'Dim Var As Variant
'Var = "select max(Autonumber) from tblAutonumber"
'DoCmd.RunSQL (Var)
txtAutoNumber.Value = strSQL
DoCmd.RunSQL ("insert into tbltesting (Empid,TestScenario,owner,event,version,expresult,variation,status,homestore)values ('" & Me.txtEmpNo.Value & "','" & Me.txtTestScenario.Value & "','" & Me.txtOwner.Value & "','" & Me.txtEvent.Value & "', '" & Me.txtVersion.Value & "','" & Me.txtExpectedResult.Value & "', '" & Me.txtVariation.Value & "', '" & Me.txtStatus.Value & "','" & Me.txtHomeStore.Value & "')")
'DoCmd.RunSQL ("INSERT INTO tblContract(testid)select max(testid) FROM tbltesting")
 DoCmd.RunSQL ("insert into tblContract (Empid,testid,Start1,Finish1,Store1,Start2,Finish2,Store2 )values ('" & Me.txtEmpNo.Value & "','" & Me.txtAutoNumber.Value & "','" & Me.txtContSunStart1.Value & "', '" & Me.txtContSunFinish1.Value & "','" & Me.txtContSunStore1.Value & "','" & Me.txtContSunStart2.Value & "', '" & Me.txtContSunFinish2.Value & "','" & Me.txtContSunStore2.Value & "')")
 'DoCmd.RunSQL = "INSERT INTO tblContract (Empid,testid, Start1, Finish1, Store1, Start2, Finish2, Store2)  SELECT " & Me.txtEmpNo.Value & "', MAX(testid), '" & Me.txtContSunStart1.Value & "', '" & Me.txtContSunFinish1.Value & "','" & Me.txtContSunStore1.Value & "','" & Me.txtContSunStart2.Value & "', '" & Me.txtContSunFinish2.Value & "','" & Me.txtContSunStore2.Value & "' " & "FROM tbltesting'"
'DoCmd.RunSQL = "INSERT INTO tblContract (Empid,testid, Start1, Finish1, Store1, Start2, Finish2, Store2)  SELECT " & Me.txtEmpNo.Value & "', MAX(testid), '" & Me.txtContSunStart1.Value & "', '" & Me.txtContSunFinish1.Value & "','" & Me.txtContSunStore1.Value & "','" & Me.txtContSunStart2.Value & "', '" & Me.txtContSunFinish2.Value & "','" & Me.txtContSunStore2.Value & "' " & "FROM tbltesting'"
End Sub
A: 

As Rich commented:

Dim Var 
Var = "select max(Autonumber) from tblAutonumber"
DoCmd.RunSQL (Var)
jdecuyper
No Joy This is the value now "select max(Autonumber) from tblAutonumber"
+1  A: 

For what you are trying to accomplish you might want to just use the DMAX Domain Aggregate Function which will return the max expression (i.e. column) for a given domain (i.e. table). Here is an example:

  Dim lastAutonumber As Long
  lastAutonumber = DMax("Autonumber", "tblAutonumber")
tchester
Thhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaksssssssssssssssssss
A: 

Using the Access UI:

Dim Var As Long
var = CurrentProject.Connection.Execute("select max(Autonumber) from tblAutonumber;")(0)
onedaywhen
To unpack that a bit, you're returning the value of index number 0 of the fields collection of the returned recordset object. I had to ponder this one for a bit to figure out why it worked. Dunno why in an Access context DMax() would not be the obvious solution, though.
David-W-Fenton
I just copied and pasted the SQL code from the question -- I didn't seek to re-write the query ;) I read that the domain functions should be avoided on performance grounds (MSDN optimization article, now disappeared) but I personally avoid them on portability grounds i.e. the SELECT MAX(... type vanilla SQL will run against any SQL product.
onedaywhen