Insert query by using VBA in Access

N

nash0503

hi

i try to insert data , which i get from a combobox ,in a access table.

i tried that in two different way.

first is:
Private Sub Combo21_Change()
Dim day As String

day = Me.Combo21.Value

DoCmd.RunSQL "INSERT INTO Date_Table (Day) VALUES (day)"

MsgBox (day)

End Sub

it prompt me to input the data for variable 'day',but i already assign the
value 'Me.Combo21.Value' to it.

And the second one is:

Private Sub Combo31_Change()
Dim db As DAO.Database, sSQL As String
Set db = CurrentDb
day = Me.Combo21.Value
MsgBox (mon)
sSQL = "INSERT INTO Date_Table (Day) VALUES (day )"
db.Execute sSQL
MsgBox (day )
End Sub

this code give me:
Run-time error '3061'
too few parameters.expected 1.

please help me.
 
A

Allen Browne

Concatente the date value from the combo into the string, including the #
delimiter:

If IsDate(Me.Combo21) Then
sSql = "INSERT INTO Date_Table ( [Day] ) VALUES (" & _
Format(Me.Combo21, "\#mm\/dd\/yyyy\#") & );"
db.Execute sSQL, dbFailOnError
Else
MsgBox "Date required"
End If

Note that DAY is a reserved word, so not a good field names. Adding square
brackets (as above) may get you out of trouble here, but it might be a good
idea to avoid these words:
http://allenbrowne.com/AppIssueBadWord.html

Also, the use of dbFailOnError lets you know if the insert doesn't work
(instead of failing silently.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top