error 3075 syntax missing comma - sql

I've looked in many places on the internet trying to solve this problem but I can't seem to figure it out. Could someone just provide a second set of eyes and just check where I may have gone wrong with the syntax?
The error says RunTime error 3075,
"Syntax error (comma) in query expression
'('test',12/15/2014','Primary')'.
Originally it worked when I had this split into four different INSERT statements, but when I combined it, it stopped working
SQL = "INSERT INTO WeeksonCall([Employee], [Week], [Prim/Backup]) VALUES (('" & _ cboName.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack.Value & _
"'),('" & cboName2.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack2.Value & _
"'),('" & cboName.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack2.Value & _
"'),('" & cboName2.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack.Value & "'))"

That is not proper syntax in MS Access. You will have to insert the records individually like:
DoCmd.SetWarnings False
SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & _ cboName.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack.Value & "')"
SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName2.Column(1) & "','" & cboWeek.Column(1) & "','" & cboPrimBack2.Value & "')"
SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack2.Value & "')"
SQL = "INSERT INTO WeeksonCall(Employee, Week, [Prim/Backup]) " & _
"VALUES ('" & cboName2.Column(1) & "','" & cboWeek2.Column(1) & "','" & cboPrimBack.Value & "')"
DoCmd.SetWarnings True
EDIT: You can stop the user prompts by using SetWarnings = False before the inserts. Then turn the warnings back on after the inserts via DoCmd.SetWarnings True.

Can you please provide the remainder of the VBA you're using in order to ask the user for input? As far as I see you're only assigning a string to the SQL variable. Where is the rest of your code?
And, by the way, I never like using:
DoCmd.SetWarnings False
Instead use the module below, that way if you do get an error you can deal with it instead of hidding it:
Option Compare Database
Option Explicit
Public Function Run_Safe_SQL(strSQL)
On Error GoTo Error_Handler
Dim db As DAO.Database
Set db = CurrentDb()
db.Execute strSQL, dbFailOnError
DBEngine.Idle dbRefreshCache
' DoEvents
Exit_Here:
'Cleanup
Set db = Nothing
strSQL = ""
Exit Function
Error_Handler:
MsgBox Err.Description & " " & Err.Number
End Function

Related

Comparing multiple combo box (text) values in an Access VBA form to prevent duplicate entries with OR condition

Putting together a query (with SQL statement) button that checks for duplicate combobox entries (within one submission) before they happen.
Trying to comprehend why this works:
ElseIf Me.pc_cbox1 = Me.pc_cbox2 Then
MsgBox "Duplicate Program Code Error"
But this does not:
ElseIf (Me.pc_cbox1 Or Me.pc_cbox2 Or Me.pc_cbox3 Or Me.pc_cbox4 Or Me.pc_cbox5 Or Me.pc_cbox6 Or Me.pc_cbox7 Or Me.pc_cbox8) =
(Me.pc_cbox1 Or Me.pc_cbox2 Or Me.pc_cbox3 Or Me.pc_cbox4 Or Me.pc_cbox5 Or Me.pc_cbox6 Or Me.pc_cbox7 Or Me.pc_cbox8) Then
MsgBox "Duplicate Program Code Error"
Edit #1: Thought I was onto something with this loop:( ...
For intComboBox = 1 To 8
If Controls("pc_cbox" & intComboBox).ListIndex <> intComboBox Then
If Controls("pc_cbox" & intComboBox).Value = Controls("pc_cbox" & intComboBox).Value Then
MsgBox "Duplicate Program Code Error"
End If
End If
Next intComboBox
Edit #2: #HansUp's suggestion works! I am going to dissect this loop for the next hour to better understand the dictionary concept. I am new to VBA (3rd week) and I know this code is mostly spaghetti at this point but I have been forced into learning as I go at work. Here is what I have put together as an add funding (percentages) per program without duplicates. 'Program Code' is part of the primary key for the SQL table and therefore will not accept duplicate entries. I wanted to prevent being able to submit duplicates on the form to nip this issue in the bud.
Private Sub fundAdd_Click()
Dim strSQL As String, queryName As String, qdf1 As QueryDef, dct As Object, i As Long, strValue As String
queryName = "temp6"
If QueryExists(queryName) Then
DoCmd.DeleteObject acQuery, "temp6"
End If
Set dct = CreateObject("Scripting.Dictionary")
For i = 1 To 8
strValue = Nz(Me.Controls("pc_cbox" & i).Value, "NULL")
If dct.Exists(strValue) Then
MsgBox "Duplicate Program Code Error"
Exit For
Else
dct.Add strValue, vbNullString
End If
Next
If Me.percentTotal <> 1 Then
MsgBox "Total not equal to 100%"
Else
strSQL = "INSERT INTO position_funding2(box_id, program_code, percent) VALUES ('" & fundboxid_cbox & "','" & pc_cbox1 & "','" & percent1 & "'), " & _
" ('" & fundboxid_cbox & "','" & pc_cbox2 & "','" & percent2 & "'), ('" & fundboxid_cbox & "','" & pc_cbox3 & "','" & percent3 & "'), " & _
" ('" & fundboxid_cbox & "','" & pc_cbox4 & "','" & percent4 & "'), ('" & fundboxid_cbox & "','" & pc_cbox5 & "','" & percent5 & "'), " & _
" ('" & fundboxid_cbox & "','" & pc_cbox6 & "','" & percent6 & "'), ('" & fundboxid_cbox & "','" & pc_cbox7 & "','" & percent7 & "'), " & _
" ('" & fundboxid_cbox & "','" & pc_cbox8 & "','" & percent8 & "');"
MsgBox (strSQL)
Set qdf1 = CurrentDb.CreateQueryDef("temp6")
qdf1.Connect = "ODBC;Driver=MySQL ODBC 8.0 Unicode Driver;SERVER=sv03rm;UID=*****;PWD=*****;DATABASE=pobe;PORT=3306;DFLT_BIGINT_BIND_STR=1"
qdf1.SQL = strSQL
qdf1.ReturnsRecords = False
DoCmd.OpenQuery "temp6"
Me.List271.Requery
End If
Defaults
End Sub
Use your combo box values as keys of a Dictionary. Before adding each of those combo values, use the Exists method to check whether that value was already stored in the Dictionary. If it does exist, the one you're about to add is a duplicate, so display your MsgBox notice.
You didn't provide any context about where and how you intend to do the comparison. So, for my version, I used a command button's click event.
Private Sub cmdCompare_Click()
Dim dct As Object
Dim i As Long
Dim strValue As String
Set dct = CreateObject("Scripting.Dictionary")
For i = 1 To 8
strValue = Nz(Me.Controls("pc_cbox" & i).Value, "NULL")
If dct.Exists(strValue) Then
MsgBox "Duplicate Program Code Error"
Exit For
Else
dct.Add strValue, vbNullString
End If
Next
End Sub

Insert textbox value from a form into a table

I have a textbox(in form) and a field(in table) both named SerialNumber
This is the control source of the textbox: =([OrderNr] & (""+[Aantal]) & "" & [SapArtNr])
I can't seem to make the textbox save the value inside into the table.
Edit:
Option Compare Database
Private Sub Command82_Click()
' Add data to Table
CurrentDb.Execute "INSERT INTO Geleidelijst(SerialNmbr, InvoerOrderNr, InvoerAantal, InvoerVermogen, InvoerHSLSSpn) " & _
" VALUES(" & Me.SerialNumberLong & ",'" & Me.OrderNr & "','" & _
Me.Aantal & "','" & Me.Vermogen & "','" & Me.HSLSSpn & "')"
End Sub
But then I get error: 3075 Synax error missing operating in query expression. And if I add date as a format it doesn't work either.
Only when it's numbers, but when there's letters and dots and "/" for example, it won't work.
Any help would be nice, thanks.
Most likely, Aantal is numeric, so try without quotes:
CurrentDb.Execute "INSERT INTO Geleidelijst " & _
"(SerialNmbr, InvoerOrderNr, InvoerAantal, InvoerVermogen, InvoerHSLSSpn) " & _
"VALUES('" & Me.SerialNumberLong & "','" & Me.OrderNr & "'," & Me.Aantal & ",'" & Me.Vermogen & "','" & Me.HSLSSpn & "')"

Insert statement terminated in VB6 [error 01000 Microsoft ODBC] when using Windows 10

My VB6 application runs OK in Windows 7 but generates an error in Windows 10. The error message is:
error 01000: microsoft ODBC
When I debug in Windows 10, Windows can't execute my insert statement. My ODBC connection is OK. I think Windows 10 can't handle long statement. Please help me solve this problem.
This is the code to save and insert data into database:
Set InsertData = New rdoQuery
Set InsertData.ActiveConnection = conndtrs
InsertData.SQL = "insert into downtime (dt_date,dt_info1,dt_shop,dt_shift, " & _
"dt_model,dt_region,dt_category,dt_sub_category,dt_time, " & _
"dt_details,dt_caseno,dt_responsible,day_night,dt_time1,user_id," & _
"dt_keyin,dt_meeting,Dt_Company,dt_ct_measure,area) " & _
" values ('" & Dt_Date & "','" & dt_info1 & "','" & dt_shop & "', " & _
"'" & dt_shift & "','" & dt_model & "','" & Dt_Region & "','" & dt_category & "', " & _
"'" & dt_sub_category & "'," & dt_time & ",'" & dt_details & "'," & newlk_caseno & ", " & _
"'" & dt_responsible & "','" & day_night & "','" & dt_time1 & "','" & user_id & "', " & _
"'" & dt_keyin & "','Y','PEMSB','" & dt_ct_measure & "','" & Area & "')"
InsertData.Execute
MsgBox ("Data saved.....")
Data is saved to a SQL 2000 database.
The problem arises in the insert query. May be the date format is wrongly specified. That is the reason this errors arises. Specify the date format as:-
Format(date,"dd/MMM/yyyy")
After turning on ODBC logging got the event message, A fatal error occurred while creating an SSL client credential. The internal error state is 10013. This did lead me to the fix from a similar error.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client\ Enabled needed to be flipped from 0 to 1

Syntax error when copying data from Excel userform to Access table

When trying to copy data from an Excel userform in to an Access table via SQL I am encountering an error on a field that is a Yes/No type in the database. How can I format the info correctly so that it updates the database?
I have looked at some other examples of copying data into the Yes/No fields in Access, as well as trying my code in a number of different formats, with double quotations, single quotations & as it appears below
Dim oConn As ADODB.Connection
Dim oCm As ADODB.Command
Dim iRecAffected As Integer
If Me.TBField.Visible = False Then
'Update office users
Init1 = Left(Me.TxtName1, 1) & Mid$(Me.TxtName1, InStr(Me.TxtName1, " ") + 1, 1)
Select Case Me.LblFraOffice.Caption
Case Is = "Add User"
Set oConn = New ADODB.Connection
oConn.Open sConn
Set oCm = New ADODB.Command
oCm.ActiveConnection = oConn
If RbUser.Value = True Then
oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, User, JobRole) " & _
"VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
ElseIf RbAdmin.Value = True Then
oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, JobRole, User) " & _
"VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
ElseIf RbManager.Value = True Then
oCm.CommandText = "INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, JobRole, User) " & _
"VALUES ('" & TxtName1.Value & "','" & TxtUname1.Value & "','" & Init1 & "','" & TxtProcess1.Value & "','" & TxtFLM.Value & "','" & TxtFLMID.Value & "', True ,'" & TxtPosition1.Value & "')"
End If
'MsgBox oCm.CommandText
oCm.Execute iRecAffected
oConn.Close
When pressing submit on the userform this should add a newly created user into the database table. If I remove the User field (and the associated true/false) it works. But my users can be users, admins or managers
At present I receive an error message
Run-time error '-2147217900 (80040e14)': Syntax error in INSERT INTO statement
Column name User is the reserved keyword. Can you add square brackets around the keyword to fix the issue:
INSERT Into Users (Name, Username, Initials, Process, FLM, FLM_ID, [User], JobRole)
or you can rename the column name to a non-reserved keyword to sovle this error.

3134 run time error on Insert into statement

I am trying to write a form to insert data into multiple tables within a single database.
I know you can't do that through a single Insert into statement so I read that I should create a transaction and include multiple Insert statements. I keep getting 3134 run time error on my second insert statement. Here is the code:
Private Sub cmdAdd_Click()
DBEngine.BeginTrans
CurrentDb.Execute "INSERT into Names(StudentId, FirstName, MiddleName, LastName) VALUES (" & Me.txtStudentId & ",'" & _
Me.txtFirstName & "','" & Me.txtMiddleName & "','" & Me.txtLastName & "')"
CurrentDb.Execute "INSERT into Homeroom(StudentId, Grade, Homeroom_Primary, Name-Homeroom_Primary_Teacher) " & _
"VALUES (" & Me.txtStudentId & ",'" & Me.txtGrade & "','" & Me.txtHomeroom & "','" & Me.txtTeacher & "')"
CurrentDb.Execute "INSERT into [Ridgeview Math](StudentId, ExportGrade, DateTaken, SS, PR) VALUES (" & _
Me.txtStudentId & ",'" & Me.txtGrade & "',#" & Me.txtMathdate & "#,'" & Me.txtMathSS & "','" & Me.txtMathPR & "')"
CurrentDb.Execute "INSERT into [Ridgeview Reading](StudentId, ExportGrade, DateTaken, RSS, RPR, RIRL) " & _
"VALUES (" & Me.txtStudentId & "','" & Me.txtGrade & "',#" & Me.txtReadingdate & "#,'" & Me.txtReadingSS & "','" & _
Me.txtReadingPR & "','" & Me.txtReadingIRL & "')"
CurrentDb.Execute "INSERT into CompassGroup(StudentId, CompassGroup) VALUES (" & Me.txtStudentId & _
"," & Me.txtCompassGroup & ")"
DBEngine.CommitTrans
End Sub
Am I doing something wrong with the nested Insert statements?
This is all tied to a form where the variables are created and the data is entered. The first Insert statement gives me no errors. Please let me know if you need more information.
Bracket the field name Name-Homeroom_Primary_Teacher because of the dash.
I suggest you use a string variable to hold the statement text, Debug.Print the string, and then Execute it.
Dim strInsert As String
strInsert = "INSERT into Homeroom(StudentId, Grade, Homeroom_Primary, [Name-Homeroom_Primary_Teacher]) " & _
"VALUES (" & Me.txtStudentId & ",'" & Me.txtGrade & "','" & Me.txtHomeroom & "','" & Me.txtTeacher & "')"
Debug.Print strInsert
CurrentDb.Execute strInsert, dbFailonerror
If case of errors, you can go to the Immediate window (Ctrl+g) to inspect the statement text. And you can copy that text, create a new query in the query designer, switch to SQL View, paste in the text, and test the statement there.
Also the approach you're using requires a whole lot of concatenating. Other options you can consider are: parameter queries; adding rows to DAO Recordsets.