Adding Record from Form to Table not Working

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I am getting an error message when I click on "Add Record" button. I am trying to add data that was entered in a form into a table.

Getting an error message with the code below. The error I am getting is "Number of query values and destination fields are not the same".

What am I doing wrong?

Code:
Private Sub AddRecord_Click()

CurrentDb.Execute "INSERT INTO MasterThroughput( [SID, Date, Case #, Entity Count, Referenced Entity #, Regional Assist?, Vendor Assist?, Fully Referenced?, LOB, Comments] )" & _
                " VALUES (" & Me.SID & ", " & Me.Date & ", " & Me.Case & ", " & Me.EntityCount & ", " & Me.ReferencedEntity & ", " & Me.RegionalAssist & ", " & Me.VendorAssist & ", " & Me.FullyReferenced & ", " & Me.LOB & ", " & Me.Comments & ")"
               
 
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Any reason why you don't want to build a form based on the table? Then you can have a new record button....However, if you want to do it like you've posted, you'll need to have brackets around individual fields in the INSERT INTO statement. Something like this:
Code:
"INSERT INTO MasterThroughput( [SID, [Date], [Case #], [Entity Count], [Referenced Entity #], [Regional Assist?], [Vendor Assist?], [Fully Referenced?], LOB, Comments )"
It isn't a great idea to name a field "Date," but if you do, you need the brackets around it (note that you may experience troubles with this field if you aren't really careful). Any fields with spaces or special characters in them need brackets.
Regarding the VALUES statement, you may need some pound signs (or hashtags - #) around Me.Date, or you may end up with odd results for that. Likewise you may need quotes around text values that you're inserting.

Have fun,
 
Upvote 0
Hello,

I made changes to the code and now I am getting run-time error '3134': Syntax error in INSERT TO statement.

Code:
Private Sub AddRecord_Click()

CurrentDb.Execute "INSERT INTO MasterThroughput( [SID], [Date], [Case #], [Entity Count], [Referenced Entity #], [Regional Assist?], [Vendor Assist?], [Fully Referenced?], [LOB], [Comments] )" & _
                " VALUES (" & Me.SID & ", # & Me.Date & #, " & Me.Case & ", " & Me.EntityCount & ", " & Me.ReferencedEntity & ", " & Me.RegionalAssist & ", " & Me.VendorAssist & ", " & Me.FullyReferenced & ", " & Me.LOB & ", " & Me.Comments & ")"
               
 
End Sub
 
Upvote 0
Hello,

I was able to get my code to work properly.

Code:
CurrentDb.Execute "INSERT INTO MasterThroughput([SID], [Date], [Case #], [Entity Count], [Referenced Entity #], [Regional Assist?], [Vendor Assist?], [Fully Referenced?], [LOB], [Comments])" & _
                "VALUES ('" & Me.SID & "', '" & Me.Date & "', '" & Me.Case & "', '" & Me.EntityCount & "', '" & Me.ReferencedEntity & "', '" & Me.RegionalAssist & "', '" & Me.VendorAssist & "', '" & Me.FullyReferenced & "', '" & Me.LOB & "', '" & Me.Comments & "');", dbFailOnError

Thank you for your input.
 
Upvote 0
Hello,

So I am having a similar issue with the code.
I just added one more column to add data to my table.
The issue I am having is that if I leave the End Date blank, I will get the runtime error 3464: Data type mismatch in criteria expression.
If I enter a date, then the code works. How can I get the code to work if the End Date is blank?

Code:
CurrentDb.Execute "INSERT INTO MasterThroughput([SID], [Start Date], [End Date], [CaseNo], [Entity Count], [Referenced Entity #], [Regional Assist?], [Vendor Assist?], [Fully Referenced?], [LOB], [Comments])" & _
                "VALUES ('" & Me.SID & "', '" & Me.StartDate & "', '" & Me.EndDate & "', '" & Me.Case & "', '" & Me.EntityCount & "', '" & Me.ReferencedEntity & "', '" & Me.RegionalAssist & "', '" & Me.VendorAssist & "', '" & Me.FullyReferenced & "', '" & Me.LOB & "', '" & Me.Comments & "');", dbFailOnError
 
Upvote 0
If you have dates that are not entered, you are going to have to do some manipulation to your string so that you can pass "Null". So maybe create a variable and do something like this:
Code:
If IsNull(Me.EndDate) Then
    strEndDate = "Null"
Else
    strEndDate = "'" & Me.EndDate & "'"
End If

CurrentDb.Execute "INSERT INTO MasterThroughput([SID], [Start Date], [End Date], [CaseNo], [Entity Count], [Referenced Entity #], [Regional Assist?], [Vendor Assist?], [Fully Referenced?], [LOB], [Comments])" & _
                "VALUES ('" & Me.SID & "', '" & Me.StartDate & "', " & strEndDate & ", '" & Me.Case & "', '" & Me.EntityCount & "', '" & Me.ReferencedEntity & "', '" & Me.RegionalAssist & "', '" & Me.VendorAssist & "', '" & Me.FullyReferenced & "', '" & Me.LOB & "', '" & Me.Comments & "');", dbFailOnError
Note that this is just an example - you could do the same thing with Start Date. So I've removed the single quotes around the value for End Date, so that we just plug in strEndDate - because if there is no End Date entered, you want to pass Null (without quotes) to your Insert Into statement.

Have fun,
 
Upvote 0
Hello,

I was getting some error messages, but I got the code to work.
Thank you so much. I didn't even think about it like you described.


Thank you,
Miriam
 
Upvote 0

Forum statistics

Threads
1,221,825
Messages
6,162,166
Members
451,750
Latest member
dofrancis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top