Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to add a new record

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I am new to Microsoft ADP files and just discovered that my code that used to work in an mdb file does not work in an adp project.

I am using the following code to try to add new records to the database

Code:
Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim var As Variant
    Set db = CurrentDb
    Set rs = db.OpenRecordset("NextTaskCriteria")
    
    
    
        
    For Each var In Me.lstSources.ItemsSelected
        With rs
            .AddNew
            .Fields("Source") = CLng(Me.lstSources.Column(0, var))
            .Fields("EmployeeID") = Me.SelectedEmployeeID
            .Update
                 
        End With
    Next
    
    MsgBox "Updated successfully...", vbInformation
    
       
    rs.Close
    db.Close
    
    
        
    Set rs = Nothing
    Set db = Nothing

I get the error Object Variable or With Block variable not set on the line Set rs = db.OpenRecordset("NextTaskCriteria")
 
That's because you don't have the object CurrentDb in an Access Data Project. You can try this:

Code:
dim var as Variant
For Each var In Me.lstSources.ItemsSelected
    doCmd.RunSQL "insert into NextTaskCriteria (Source, EmployeeID) values (" _
        & CLng(Me.lstSources.Column(0, var)) & ", " & Me.SelectedEmployeeID & ");"
Next
 
Worked perfectly, Thank you very much. I had to remove the line Set rs = db.OpenRecordset("NextTaskCriteria") and also rs.Close then it worked
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top