Vba Insert into tbl (1 Viewer)

Pete Printer

Registered User.
Local time
Today, 21:23
Joined
Jul 28, 2014
Messages
15
Hi

This code works fine and inserts the 3 values into the table.
Code:
DoCmd.RunSQL "INSERT INTO JobsStaffAvail ( tblJobsID, tblStaffAvailID, bookedBy )" _
    & "VALUES(Forms!frmbookstafftojob.JobsID ,Forms!frmbookstafftojob.subfrmStaffAvail.Form.staffid ,'TestData')"

However i need to replace the TestData value with the current user details.
i created a Variable BookedBy as string and set it thus.
Code:
BookedBy = Environ$("username")

The variable is set fine
i have tried replacing TestData with BookedBy and this throws error end of statement required.
Samething happens when i try
Code:
 '(Environ$("username"))'

Thanks for looking at this.:banghead:
Pete
 

Minty

AWF VIP
Local time
Today, 21:23
Joined
Jul 26, 2013
Messages
10,371
Are you sure this works ? I didn't think it would interpret the references to your forms
Code:
DoCmd.RunSQL "INSERT INTO JobsStaffAvail ( tblJobsID, tblStaffAvailID, bookedBy )" _
    & "VALUES(Forms!frmbookstafftojob.JobsID ,Forms!frmbookstafftojob.subfrmStaffAvail.Form.staffid , BookedBy)"

It's much better for debugging to build the SQL string separately ;
Code:
BookedBy = Environ$("username")
sSql = "INSERT INTO JobsStaffAvail ( tblJobsID, tblStaffAvailID, bookedBy ) " 
sSql = sSql & "VALUES (" & Forms!frmbookstafftojob.JobsID & " , " 
sSql = sSql &  Forms!frmbookstafftojob.subfrmStaffAvail.Form.staffid & " , '"
sSql = sSql &  BookedBy & "' );"

Debug.Print sSql  [COLOR="YellowGreen"] ' This will show you what it's created with your values[/COLOR]

CurrentDb.Execute sSql, dbSeecChanges
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:23
Joined
May 7, 2009
Messages
19,247
DoCmd.RunSQL "INSERT INTO JobsStaffAvail ( tblJobsID, tblStaffAvailID, bookedBy )" _
& "VALUES(" & Forms!frmbookstafftojob.JobsID & "," & _
Forms!frmbookstafftojob.subfrmStaffAvail.Form.staffid & ",'" & _
Environ("username") & "')"
 

JANR

Registered User.
Local time
Today, 22:23
Joined
Jan 21, 2009
Messages
1,623
Re: Vba Insert into tbl
Are you sure this works ? I didn't think it would interpret the references to your forms

DoCmd.RunSQL "INSERT INTO JobsStaffAvail ( tblJobsID, tblStaffAvailID, bookedBy )" _
& "VALUES(Forms!frmbookstafftojob.JobsID ,Forms!frmbookstafftojob.subfrmStaffAvail.Form.staffid , BookedBy)"

Yes Minty, you can do it like OP did, provided you use DoCmd.RunSql and not execute methode. RunSQL gets evalueted by Access at runtime and not dumped into the engine like Execute does.

Janr
 

Pete Printer

Registered User.
Local time
Today, 21:23
Joined
Jul 28, 2014
Messages
15
Minty
Thanks for the prompt reply.
You code works fine thank you.
i can see the logic in building the statement this way.
That is very useful advice as well.

Best regards
Pete
 

Pete Printer

Registered User.
Local time
Today, 21:23
Joined
Jul 28, 2014
Messages
15
JANR
thanks for that its working fine now.

Great forum this is
Pete
 

Users who are viewing this thread

Top Bottom