insert unbound field values into a value list to update table

Draw a blank on the proper syntax for an insert statement using variables.

gCountryID = Nz(Forms![frmHomePage]![Add New Location].Form![Countryrecid])
gStateID = Nz(Forms![frmHomePage]![Add New Location].Form![StateRecID])
gCityName = Nz(Forms![frmHomePage]![Add New Location].Form![CityRecID])
strSQL = "INSERT INTO tblCity ( CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby )" & _
            " VALUE (gcountryID,Gstateid], gcityname, gTaxIncl, gtop, date(),GetgUserID())"
Debug.Print strSQL

Open in new window

Please feel free to make corrects.  I am using an unbound form and on enter of each field a variable is set.

Karen SchaeferBI ANALYSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeCommented:
'Note that I like to put my ampersands at the beginning of each line, and prefer to add spaces at the end of a line
'This assumes that GStateID, gCityName, and the function GetgUserId() are all text fields
strSQL = "INSERT INTO tblCity (CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby) " _
       & "VALUE (" & gcountryID & ", " _
                   & chr$(34) & Gstateid & chr$(34) & ", " _
                   & chr$(34) & gcityname & chr$(34) & ", " _
                   & gTaxIncl & ",  " _
                   & gtop & ", " _
                   & "#" & date() & "#, " _
                   & chr$(34) & GetgUserID() & chr$(34) _
                   & ")"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOCommented:
You can and should insert Date() as is, and it is VALUES:
strSQL = "INSERT INTO tblCity ( CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby ) " & _
    "VALUES ( '" & gcountryID & "', '" & Gstateid & "', '" & gcityname & "', " & gTaxIncl & ", " & gtop & ", Date(), '" & GetgUserID() & "' )"

Open in new window

Karen SchaeferBI ANALYSTAuthor Commented:
getting there, but still having issue returning and updating the UserLoginID from the GetUserName function.
Function GetUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngX = apiGetUserName(strUserName, lngLen)
    If (lngX > 0) Then
        GetUserName = Left$(strUserName, lngLen - 1)
        GetUserName = vbNullString
    End If
End Function
Public Function GetgUserID() As String
    GetgUserID = Nz(DLookup("UserLoginID", "tblUser", "UserName" = GetUserName()))
Debug.Print GetgUserID
End Function

Open in new window

INSERT INTO tblCity ( CountryRecID, StateRecID, CityName, TaxInclusive, Top5000, DateCreated, Modifiedby ) VALUES ( '41', '31', 'testadfadf', False, False, Date(), '' )

Open in new window

The current value is having an issue with Null on GetuserID?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Gustav BrockCIOCommented:
I think it should read:

GetgUserID = Nz(DLookup("UserLoginID", "tblUser", "UserName = GetUserName()"))


GetgUserID = Nz(DLookup("UserLoginID", "tblUser", "UserName = '" & GetUserName() & "'"))

Karen SchaeferBI ANALYSTAuthor Commented:
Gustav BrockCIOCommented:
You are welcome!

Dale FyeCommented:
Glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.