How do I concatenate a variable of the type Date/Time in my Insert statement - sql

This runs when the form loads:
Private Sub Form_Load()
TempVars("F_ID") = txtF_SNr.Value
TempVars("Status") = txtStatus.Value
TempVars("seit") = txtSeit.Value
TempVars("bis") = txtBis.Value
TempVars("von") = txtVon.Value
TempVars("an") = txtAn.Value
TempVars("Bemerkung") = txtBemerkung.Value
End Sub
txtSeit is the name of the text field in my form, bound to the column [seit] with the type of Date/Time in my table.
sql = "Insert into tblStatusFahrzeugeHistory (F_ID, Status, seit, bis, von, an, Bemerkung, Datenbank_Nutzer, Eintrag_erstellt_am, Eintrag_erstellt_um)" & _
"Select tblStatusFahrzeuge.F_ID, tblStatusFahrzeuge.Status, tblStatusFahrzeuge.seit, tblStatusFahrzeuge.bis, tblStatusFahrzeuge.von, tblStatusFahrzeuge.an, tblStatusFahrzeuge.Bemerkung, tblStatusFahrzeuge.Datenbank_Nutzer, tblStatusFahrzeuge.Eintrag_erstellt_am, tblStatusFahrzeuge.Eintrag_erstellt_um " & _
"From tblStatusFahrzeuge Where ( " & txtSeit.Value & " <> seit AND [F_ID] = '" & Me.txtF_SNr.Value & "') "
The sql statement above gives me the error:
runtime error: '3075' syntax error in number in query expression...
How do I concatenate txtSeit.Value correctly? (Sorry im a beginner!)

Related

How to find a syntax error in my SQL code?

I am trying to compile my code but I get the same error every time:
Syntax error (missing operator ) in query expression
' True Status.Subsystem Not LIKE '''
This is my code :
Sub Import_Loop_Check_list()
Dim strSQL As String
Dim SS_sel As String
Dim rcrd As DAO.Recordset
If IsNull(Cobsubsystem) Then
SS_sel = "True"
Else
If IsNull(Logic1) Then
SS_sel = "Status.Subsystem LIKE '" & Cobsubsystem & "' "
Else
SS_sel = "Status.Subsystem NOT LIKE '" & Cobsubsystem & "' "
End If
End If
strSQL = " SELECT DISTINCT LOOP_JB.Loop_name, [Easyplant Dump query].Subsystem, LOOP_JB.PANEL_FROM, LOOP_JB.ITR_PANEL_FROM, LOOP_JB.ITR_PANEL_FROM_state, LOOP_JB.CABLE_NUM, LOOP_JB.ITR_cable, LOOP_JB.ITR_STATE_Cable, LOOP_JB.Cabinet_JB, LOOP_JB.ITR_Cabinet_JB, LOOP_JB.ITR_STATE_CABINET, Multicors.CABLE_NUM AS Multicore, Multicors.ITR_PANEL_FROM, Multicors.ITR_PANEL_FROM_state, [Cabinet query].PANEL_TO, [Cabinet query].ITR_PANEL_TO, [Cabinet query].ITR_PANEL_TO_state INTO [LOOP_Check] " & _
" FROM (LOOP_JB INNER JOIN ([Cabinet query] RIGHT JOIN Multicors ON [Cabinet query].CABLE_NUM = Multicors.CABLE_NUM) ON LOOP_JB.Loop_name = Multicors.Loop_name) INNER JOIN [Easyplant Dump query] ON LOOP_JB.Loop_name = [Easyplant Dump query].Clean_Tag_Number" & _
" WHERE True " & SS_sel & strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.OpenTable "LOOP_Check"
End Sub
I looked at this again -- something else does not make sense.
You reference Status.Subsystem in the WHERE but there is no table named Status -- Did you not include the full query?
original answer
I think the error message is clear -- you have a strange where statement
WHERE True Status.Subsystem Not LIKE
you probably mean
WHERE Status.Subsystem Not LIKE
so change this line
" WHERE True " & SS_sel & strSQL
to this
" WHERE " & SS_sel & strSQL
Also, it does not right to me -- are you sure you want to do a RIGHT join to Multicors and not a left join? You want a row in your result for every row in the multicors table?

Looping through table to find value from a textbox

I'm trying to loop through a column inside a table from a form in Access to find out whether a "Case Name" already exists or not, and if it does not, then add the new record to the table. I want the criteria to be based on the input value of a text box. The good news is I have figured out how to add a new record to the table with the code below. I'm just stuck on how to loop through a table to find out if a record already exists. Thanks in advance!
Private Sub SaveNewCase_Click()
If Me.txtNewCaseName.Value <> "Null" And Me.txtCaseDepth.Value <> "Null" And Me.txtCaseHeight2.Value <> "Null" And Me.txtCaseWeight.Value <> "Null" And Me.txtCaseWidth <> "Null" And Me.cboCaseCategory.Value <> "Null" Then
'I think the loop should go here, but not sure'
CurrentDb.Execute "INSERT INTO tblCases(CaseName, CaseWidth, CaseHeight, CaseCasters, CaseWeight, CaseDepth, CaseCategory) " & _
" VALUES ('" & Me.txtNewCaseName & "'," & Me.txtCaseWidth & "," & Me.txtCaseHeight2 & ",'" & Me.chkboxCasters & "'," & Me.txtCaseWeight & "," & Me.txtCaseDepth & ",'" & Me.cboCaseCategory & "')"
Else
MsgBox "Please enter all new case criteria."
End If
End Sub
Firstly, use parameters!
Concatenating values supplied by a user directly into your SQL statement exposes your to SQL injection, either intentional (i.e. users entering their own SQL statements to sabotage your database) or unintentional (e.g. users entering values containing apostrophes or other SQL delimiters).
Instead, represent each of the field values with a parameter, for example:
With CurrentDb.CreateQueryDef _
( _
"", _
"insert into " & _
"tblcases (casename, casewidth, caseheight, casecasters, caseweight, casedepth, casecategory) " & _
"values (#casename, #casewidth, #caseheight, #casecasters, #caseweight, #casedepth, #casecategory) " _
)
.Parameters("#casename") = txtNewCaseName
.Parameters("#casewidth") = txtCaseWidth
.Parameters("#caseheight") = txtCaseHeight2
.Parameters("#casecasters") = chkboxCasters
.Parameters("#caseweight") = txtCaseWeight
.Parameters("#casedepth") = txtCaseDepth
.Parameters("#casecategory") = cboCaseCategory
.Execute
End With
Since the value of each form control is fed directly to the parameter within the SQL statement, the value will always be interpreted as a literal and cannot form part of the SQL statement itself.
Furthermore, you don't have to worry about surrounding your string values with single or double quotes, and you don't have to worry about formatting date values - the data is used in its native form.
Where testing for an existing value is concerned, you can either use a domain aggregate function, such as DLookup, or you could use a SQL select statement and test that no records are returned, e.g.:
Dim flg As Boolean
With CurrentDb.CreateQueryDef _
( _
"", _
"select * from tblcases where " & _
"casename = #casename and " & _
"casewidth = #casewidth and " & _
"caseheight = #caseheight and " & _
"casecasters = #casecasters and " & _
"caseweight = #caseweight and " & _
"casedepth = #casedepth and " & _
"casecategory = #casecategory " _
)
.Parameters("#casename") = txtNewCaseName
.Parameters("#casewidth") = txtCaseWidth
.Parameters("#caseheight") = txtCaseHeight2
.Parameters("#casecasters") = chkboxCasters
.Parameters("#caseweight") = txtCaseWeight
.Parameters("#casedepth") = txtCaseDepth
.Parameters("#casecategory") = cboCaseCategory
With .OpenRecordset
flg = .EOF
.Close
End With
End With
If flg Then
' Add new record
Else
' Record already exists
End If
Finally, you're currently testing the values of your form controls against the literal string "Null", which will only be validated if the user has entered the value Null into the control, not if the control is blank.
Instead, you should use the VBA IsNull function to check whether a variable holds a Null value.

SQL query works in access but not in excel

I need to get data from a recordset. The SQL query works fine in MS Access and returns exactly the expected values, but when the same query is lunched in VBA Excel, I get the following error:
No value given for one or more required parameters
Do you have any ideas why this problem occurs?
Thank you.
Philippe-Olivier Roussel
Private Sub CBtype_AfterUpdate()
Dim strConnexion As String
Dim connexion As New ADODB.Connection
strConnexion = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Database & ""
connexion.Open strConnexion
Dim rsMarque As New ADODB.Recordset
Dim seltype As String
seltype = CBtype.Value
rsMarque.Open "SELECT DISTINCT tblMarque.marque_nom FROM tblMarque, tblModele WHERE " & _
" tblMarque.marque_id = tblModele.marque_id AND tblModele.marque_id IN " & _
" (SELECT DISTINCT tblModele.marque_id FROM tblModele, tblType " & _
" WHERE tblModele.type_id = tblType.type_id AND tblModele.type_id = " & _
" (SELECT tblType.type_id FROM tblType WHERE " & _
" (tblType.type_nom = " & seltype & ")))", connexion, adOpenStatic
rsMarque.MoveFirst
With UserForm2.CBmarque
.Clear
Do
.AddItem rsMarque!marque_nom
rsMarque.MoveNext
Loop Until rsMarque.EOF
End With
End Sub
This error message looks like an output from the DBMS rather than Excel
I think you might be missing the apostrophe before and after your string variable. See if
" (tblType.type_nom = '" & seltype & "')))" works (I'm assuming the column you're querying is varchar type, since you declared seltype as string)

Run time error 424 .. object required

I am getting this error in Microsoft Access:
runtime error 424 object required
And below is the VBA code that I have used on the backend of a button:
VBA Code
Private Sub Command87_Click()
If Me.phase <> "" Then
Me.lst_caseItems.RowSource = " " & _
"SELECT tbl_master.fld_masterID, " & _
"tbl_master.fld_masterActionNo AS ActionNumber, " & _
"tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _
"tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _
"FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = ' " & tbl_studies.fld_studyID & " ') LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = ' " & tbl_phases.fld_phaseID & " ') LEFT JOIN tbl_locations ON tbl_master.fld_locationID = ' " & tbl_locations.fld_locationID & " ';"
End If
End Sub
In your current code, VBA thinks tbl_studies.fld_studyID is a variable hence asking for an object.
With your current code;
' " & tbl_studies.fld_studyID & " '
The " CLOSES the SQL statement, then your ampersand tbl_studies or etc tells VBA to add in the value of the variable tbl_studies.fld_studyID then the ampersand followed by " reopens the SQL statement within VBA (so on compile all flows as necessary). As you have no variable named studyID it looks like this is the cause of your object required.
To get around this, you either need to assign a variable to the ID you want (such as dim X as long then x = studyIDyouwanthere) or write your query correctly so it links to the studies table with field studyID. Looking at it more closely it looks like you just need to write the query properly in the VBA window so it's syntactically correct (in VBA's interpretation) try the following;
Private Sub Command87_Click()
If Me.phase <> "" Then
Me.lst_caseItems.RowSource = " " & _
"SELECT tbl_master.fld_masterID, " & _
"tbl_master.fld_masterActionNo AS ActionNumber, " & _
"tbl_master.fld_masterStudyActionNo AS StudyActionNo, tbl_master.fld_masterIssuedTo," & _
"tbl_master.fld_masterRev AS Rev, tbl_phases.fld_phase AS Phase, tbl_studies.fld_study AS Study, " & _
"FROM ((tbl_master LEFT JOIN tbl_studies ON tbl_master.fld_studyID = tbl_studies.fld_studyID) LEFT JOIN tbl_phases ON tbl_master.fld_phaseID = tbl_phases.fld_phaseID ) LEFT JOIN tbl_locations ON tbl_master.fld_locationID = tbl_locations.fld_locationID;"
End If
End Sub
When you are joining on queries in VBA you don't join based on the values of variables, you'll only be using them for your select part or having/where/group BY not on the joins.

Microsoft Access run-time error '3075' Syntax Error

I want to delete something in my database using a button with the following VBA behind it.
Unfortunately it gives me a run-time error and I don't know why. I've looked how Access give the SQL statement back and in SQL Server it gives me no errors.
VBA:
Private Sub btnDeleteTaak_Click()
If Not IsNull(Me.lstTaakMonteur) Then
If Not IsNull(Me.comboMonteur.Value) Then
If Not IsNull(Me.comboOpdracht.Value) Then
Dim DeleteSQL As String
DeleteSQL = "DELETE OpdrachtTaak WHERE opdrachtnr = " & Me.comboOpdracht.Value & " AND taaknr = " & Me.lstTaakMonteur.Value & " AND monteurcode = '" & Me.comboMonteur.Value & "'"
DoCmd.RunSQL (DeleteSQL)
Me.lstTaakMonteur.Requery
Else
MsgBox ("Er is geen opdracht geselecteerd")
End If
Else
MsgBox ("Er is geen monteur geselecteerd")
End If
Else
MsgBox ("Er is geen taak geselecteerd")
End If
End Sub
The error:
Syntax error (missing operator) in query expression 'OpdrachtTaak WHERE opdrachtnr = 1 AND taaknr = 6 AND monteurcode = 'LM1"
Table structure OpdrachtTaak:
opdrachtnr NUMERIC(5) <pk,fk2> NOT NULL
taaknr NUMERIC(3) <pk, fk3> NOT NULL
tijdsduur NUMERIC(4,1) NULL
Monteurcode VARCHAR(3) <fk1> NULL
Would be great if you can help me out.
Delete statements in MS Access databases is fairly different from those in SQL Server.
In MS Access you must include From statement in your delete query. So your code will be:
DeleteSQL = "DELETE * FROM OpdrachtTaak WHERE opdrachtnr = " & Me.comboOpdracht.Value & " AND taaknr = " & Me.lstTaakMonteur.Value & " AND monteurcode = '" & Me.comboMonteur.Value & "'"