INSERT INTO *simple question*

Steff_DK

Registered User.
Local time
Today, 17:58
Joined
Feb 12, 2005
Messages
110
I searched the forum but can't find an example of "insert into" where the values aren't taken from another table or query.

It's very simple:
I have a function that assigns some string variables, str1, str2 etc.
After that I want to insert them into a new post in my table, Table1.
This doesn't work, but how do I do???
Code:
SQL="INSERT INTO Table1(name, time) str1, str2;"
DoCmd.RunSQL SQL
 
This works:

Code:
SQL="INSERT INTO Table1(name, time) VALUES('stringwhatever', 'another string');"
DoCmd.RunSQL SQL
 
Firstly - "name" and "time" are inadvisable choices for field names.
refer this link for more information (for some reason I can't get the link function to work, but here is the URL - it is a thread on this forum.)

http://www.access-programmers.co.uk/forums/showthread.php?t=86491

secondly - Take a look at the example below

Code:
Sub doit()
    Dim szSQL As String
    Dim szHello As String
    Dim szWorld As String
    
    szHello = "hello"
    szWorld = "world"
    
    szSQL = "INSERT INTO tblMyTableName ( MyFirstFieldName, MySecondFieldName ) SELECT " & """" & szHello & """" & " AS TheFirstField, " & """" & szWorld & """" & " AS TheSecondField;"
    'Debug.Print szSQL
    DoCmd.RunSQL szSQL

End Sub

and adapt to suit
 
True :p
- I don't use those names in my db. Just needed something short 'n' sweet to call it in the example ;)

Thanks!

Steff
 

Users who are viewing this thread

Back
Top Bottom