Neew help with a query in a database using DAO 3.6

Neutrall

New Member
Joined
Mar 16, 2015
Messages
1
Hi :),

I have the following code : to test the entry in a database :

Code:
Sub test()
Dim x As Integer
    x = Add_NewBatch_DB(125, "12/05/2011", "20/06/2015", "20/06/2015")
End Sub


Private Function Add_NewBatch_DB(CptMach As Long, TmpSupp As Date, TmpNP As Date, CurrDte As Date) As Integer
'Function will return the ID number of the newly inserted batch

    Dim MainData As Database        'Database Connection
    Dim Batch As Recordset          'Table connection
    Dim SQL As String

    'Open Database and open the recordset
    Set MainData = OpenDatabase(ActiveWorkbook.Path & DB_FILE_PATH_NAME)
    Set Batch = MainData.OpenRecordset("tbl_result_batch")
    
    Batch.AddNew
    Batch!CompteurMachine = CptMach
    Batch!TempSupplementaire = TmpSupp
    Batch!TempNonProductif = TmpNP
    Batch!CurrDate = CDate(CurrDte)
    Batch.Update
   
    SQL = "SELECT ID FROM tbl_result_batch WHERE CurrDate=" & CDate(CurrDte) & " AND CompteurMachine =" & CptMach
    Set Batch = MainData.OpenRecordset(SQL, dbOpenDynaset)
    
    MsgBox Batch!ID
    
    'Close connection
    Batch.Close
    MainData.Close
    Set Batch = Nothing
    Set MainData = Nothing
End Function


My goal here if to to able to retreive the ID Field of the last inserted element with my function "Add_NewBatch_DB"

But when I enter " WHERE CurrDate=" & CDate(CurrDte) & " " in my SQL command, I get no result. If I add the " ' " before and after the date, I get a invalid argument response.

How Can I :

1- Get the value of the ID field of the last inserted element (Auto increment field)
2- Use Date in my SQL request?

Thank you!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,214,553
Messages
6,120,176
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top