ACCESS2013 - Run-time error '3061'. Too few parameters. Expected 1 - ms-access

I got en error while opening my form in access.
This code should be executed with the "OnOpen event" for that form.
But im getting an error in my script.
Dim ThisDB As DAO.Database
Set ThisDB = CurrentDb
Dim d As DAO.Recordset
Dim q As String
q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2 'sql query
Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
Dim Result As String
Result = ""
If d.EOF = False Or d.BOF = False Then 'if-else clause
d.MoveFirst
Do While Not d.EOF
If Result <> "" Then Result = Result & "; "
Result = Result & d!EMail
d.MoveNext
Loop
End If
d.Close
The faulty line is:
Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)

I solved the problem.
The Line
q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & sid2
was not correct.
q = "SELECT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[SID] = " & "'" & sid2 & "'"
The difference is: = " & "'" & sid2 & "'"

Related

Querying Access from VBA

I am currently trying to write a query in VBA to obtain data in Access. For the string SQL, everything works except for the WHERE clause and I am not sure how to write it syntax wise. I am getting a
Run-Time error 3075 - Syntax error (missing operator) in query
expression.
Below is my code:
Sub ImportFromAccess_Size()
database_file_path = Range("Database_File_Path").Value
Dim DbLoc As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim xlbook As Workbook
Dim xlsheet As Worksheet
Dim recCount As Long
Dim SQL As String
DbLoc = database_file_path
Application.ScreenUpdating = False
Set xlbook = ActiveWorkbook
Set xlsheet = Sheets("Simulation Input Data")
Application.StatusBar = "Connecting to an external database..."
'Application.Cursor = xlWait
tool_file_path = Range("Tool_File_Path").Value
database_file_path = Range("Database_File_Path").Value
size_system = Range("Size_System").Value
Set db = OpenDatabase(DbLoc)
'Set db = OpenDatabase(database_file_path)
SQL = "SELECT SCHED_SURGERY_DATETIME "
SQL = SQL & "FROM Raw_Data "
SQL = SQL & "ORDER BY SCHED_SURGERY_DATETIME "
SQL = SQL & "WHERE [PROCEDURE_NM_WID] = (" & size_system & ")"
Sheets("Simulation Input Data").Activate
Set rs = db.OpenRecordset(SQL, dbOpenSnapshot)
xlsheet.Range("A2").CopyFromRecordset rs
End Sub
Put the text string criteria in single quotes (aka 'ticks')
SQL = "SELECT [SCHED_SURGERY_DATETIME] "
SQL = SQL & "FROM Raw_Data "
SQL = SQL & "WHERE [PROCEDURE_NM_WID] = '" & size_system & "' "
SQL = SQL & "ORDER BY [SCHED_SURGERY_DATETIME];"

VBA Update query for multi fields

I have been trying to fix this code, but have no idea why I keep having too few parameters :1 error
strSQL = "UPDATE tblProduct "
strSQL = strSQL & "SET [Verified_By] = " & Me.txtCurrentUser & "" & " , [Verified_Date] = #" & Me.txtAuto_Date & "#" & ", [Status] = ""Verified"""
strSQL = strSQL & " WHERE [Status] = ""Not Verified""" & " AND [Verify] = -1"
I am still having troubles with the syntax for attaching variables to queries in VBA. The query works in my UPDATE statement in Access.
This is what I use in Access query:
UPDATE tblProduct SET Verified_By = forms!frmVerificationProduct!txtcurrentuser, Verified_date = forms!frmVerificationProduct!txtAuto_date, Status = "Verified"
WHERE verify = -1 AND Status = "Not Verified";
If the tblProduct.Verified_By field is text datatype, the problem is that the UPDATE is supplying an unquoted text value. And when Access sees the unquoted text value, it assumes that must be a parameter for which you have not supplied a value.
In that case, you could revise the code to include the needed quotes or switch to a parameter query and not be bothered with quotes ...
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
strSQL = "UPDATE tblProduct" & vbCrLf & _
"SET [Verified_By] = [pCurrentUser], [Verified_Date] = [pAuto_Date], [Status] = 'Verified'" & vbCrLf & _
"WHERE [Status] = 'Not Verified' AND [Verify] = -1"
Debug.Print strSQL ' <- view in Immediate window; Ctrl+g will take you there
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, strSQL)
qdf.Parameters("pCurrentUser").Value = Me!txtCurrentUser.Value
qdf.Parameters("pAuto_Date").Value = Me!txtAuto_Date.Value
qdf.Execute dbFailOnError
This should do:
strSQL = "UPDATE tblProduct "
strSQL = strSQL & "SET [Verified_By] = " & Me.txtCurrentUser & ", [Verified_Date] = #" & Format(Me.txtAuto_Date, "yyyy\/mm\/dd") & "#, [Status] = 'Verified' "
strSQL = strSQL & "WHERE [Status] = 'Not Verified' AND [Verify] = -1"

Access "Runtime error 3464" Data type mismatch in criteria expression

Upon clicking a drop down menu and section of an entry that I have put in there, I get runtime error 3464 which is data type mismatch and stops at Set rsrecall = dbsrecall.OpenRecordset(strSQLWork)
What am I missing here?
Dim dbsrecall As DAO.Database
Dim rsrecall As DAO.Recordset
Dim intRecCnt As Integer
On Error GoTo Err_Click
strSQLWork = "SELECT tblAB.ID, .,.(lots)...., FROM tblAB WHERE tblAB.Title = " & Me.cmbGetRecall & " ORDER BY tblAB.CreationDate, tblAB.SolutionTarget, tblAB.StartDate;"
Set dbsrecall = CurrentDb()
Set rsrecall = dbsrecall.OpenRecordset(strSQLWork)
rsrecall.MoveFirst
ReDim arrRecall(1, 70)
arrRecall(1, 1) = rsrecall!abc
arrRecall(1, 2) = rsrecall!def
.
.(contd.)
.
arrRecall(1,70) = rsrecall!xyz
Me.txtTitle.SetFocus
Me.lblRecall.Visible = False
Me.cmbGetRecall.Visible = False
Me.txtqwe = arrRecall(1, 4)
Me.txtrty = arrRecall(1, 5)
Me.txtuio = arrRecall(1, 6)
.
.(contd.)
.
me.txtghj = arrRecall(1,70)
Exit Sub
Err_Click:
resp = MsgBox("No records were found for this selection." & Chr(10) & Chr(13) & Chr(10) & Chr(13) & "Please try again.", vbOKOnly)
Me.cmbSol = ""
Me.cmbSol.SetFocus
try
strSQLWork = " SELECT tblAB.ID, .,.(lots)...., FROM tblAB " & _
" WHERE tblAB.Title = '" & Me.cmbGetRecall & "'" & _
" ORDER BY tblAB.CreationDate, tblAB.SolutionTarget, tblAB.StartDate;"

Populate multiple textboxs from openrecordset on Access form

I am trying to populate multiple textboxs from an openrecordset and getting the following error
Run-Time error 3601
Too few parameters. Expected 1
Here is my function
Function fnSearchAndPopulate() As Boolean
Dim d As DAO.Database, r As DAO.Recordset, strSQL As String
Set d = CurrentDb
If Me.txtEnterNumber = "" Then
MsgBox "Please Enter Number", , "Error"
Exit Function
End If
strSQL = "SELECT * FROM amipartnumbers Inner Join jdsubs on amipartnumbers.oemitem=jdsubs.oempartnumber WHERE " & txtEnterNumber.Value & " In (jdsubs.oempartnumber, jdsubs.oemsubnumber)"
Set r = d.OpenRecordset(strSQL)
If r.EOF Then
MsgBox "BAM # " & Me.txtEnterNumber & " does not exist!", , "No BAM #"
Set d = Nothing
Exit Function
End If
'get here if there is a record
r.MoveFirst
'populate whatever textboxes
Me.txtAMINumber = r!Item
Me.txtDescription = r!Description
Me.txtOEMsubnumber = r!OEMsubnumber
Set d = Nothing
Exit Function
End Function
Updated for non-numeric part numbers...
strSQL = " SELECT * FROM amipartnumbers Inner Join jdsubs on " & _
" amipartnumbers.oemitem=jdsubs.oempartnumber WHERE " & _
" jdsubs.oempartnumber= '" & txtEnterNumber.Value & "' or " & _
" jdsubs.oemsubnumber= '" & txtEnterNumber.Value & "'"

More efficinet way to filter form

I have the following code:
Public Function BuildSQL(stQueryName As String, stWhereClause As String) As String
On Error GoTo Err_BuildSQL
Dim SQLcmd As String
Dim intPos As Integer
Dim db As Database
Dim qryOrig As QueryDef
Set db = CurrentDb()
Set qryOrig = db.QueryDefs(stQueryName)
SQLcmd = qryOrig.SQL
intPos = InStr(SQLcmd, "WHERE")
If intPos > 0 Then
SQLcmd = Left(SQLcmd, intPos - 1)
End If
intPos = InStr(SQLcmd, ";")
If intPos > 0 Then
SQLcmd = Left(SQLcmd, intPos - 1)
End If
If Not (stWhereClause = "") Then
SQLcmd = Trim(SQLcmd) & " WHERE " & stWhereClause & ";"
Else
SQLcmd = Trim(SQLcmd) & ";"
End If
BuildSQL = SQLcmd
Exit_BuildSQL:
Set qryOrig = Nothing
Set db = Nothing
Exit Function
Err_BuildSQL:
MsgBox Err.Description
Resume Exit_BuildSQL
End Function
Private Sub SandBox_Click()
On Error GoTo Err_SandBox_Click
Dim db As Database
Dim rs As Recordset
Dim stSQL As String
Dim stFrmName As String
Dim stQryName As String
Dim stSQLWhere As String
Dim stIDList As String
stFrmName = "Libri"
stQryName = "Libri_All_Query"
'Define WHERE clause
stSQLWhere = ""
If Not (IsNull([Forms]![Libreria]![Editore]) Or [Forms]![Libreria]![Editore] = "") Then
stSQLWhere = stSQLWhere & "Libri_Editori.Editore = '" & [Forms]![Libreria]![Editore] & "'"
End If
If Not (IsNull([Forms]![Libreria]![CognomeAutore]) Or [Forms]![Libreria]![CognomeAutore] = "") Then
If (stSQLWhere = "") Then
stSQLWhere = stSQLWhere & "Autori.Cognome = '" & [Forms]![Libreria]![CognomeAutore] & "'"
Else
stSQLWhere = stSQLWhere & " AND Autori.Cognome = '" & [Forms]![Libreria]![CognomeAutore] & "'"
End If
End If
'Here several more fields of the search form will be checked and added
stSQL = BuildSQL(stQryName, stSQLWhere)
'*** Code in question!
Set db = CurrentDb()
Set rs = db.OpenRecordset(stSQL)
If Not (rs.EOF And rs.BOF) Then
stIDList = "("
rs.MoveFirst
Do Until rs.EOF = True
If (stIDList = "(") Then
stIDList = stIDList & rs.Fields(0)
Else
stIDList = stIDList & ", " & rs.Fields(0)
End If
rs.MoveNext
Loop
stIDList = stIDList & ")"
Else
Err.Description = "Errore! Recordset vuoto."
Resume Err_SandBox_Click
End If
DoCmd.OpenForm stFrmName, , , , acFormReadOnly
Access.Forms(stFrmName).RecordSource = "SELECT * FROM Libri WHERE Libri.ID IN " & stIDList
'**** End code in question
Exit_SandBox_Click:
Set db = Nothing
Set rs = Nothing
Exit Sub
Err_SandBox_Click:
MsgBox Err.Description
Resume Exit_SandBox_Click
End Sub
This code works as I want but "looks" slow even with a test DB with only a few records in each table.
I believe the time is spent (how can I check if this is true?) in the loop between comments.
Is there a more basic, obvious and efficient way to filter the form than creating a recordset and looping through it as I am doing?
The form "Libri" is a big one with several subform to be able to see all the data of a Book.
The query "Libri_All_Query" is a join of almost all tables in the DB and the code shown is executed from a form where I plan to add all possible search fields.
Forms have a filter property:
stWhereClause = "Title Like '" & Me.txtSearch & "*'"
Me.Filter = stWhereClause
Me.FilterOn = True
The filter should be constructed in a similar way to a WHERE statement. There are some limitations compared with Where. You may wish to check with DCount that records will be returned.
EDIT
If you want a set of records where a subform contains only certain records, you need something on these lines:
SELECT b.Title
FROM Books b
WHERE b.ID IN (
SELECT j.BookID FROM BooksAuthorJunction j
INNER JOIN Authors a ON j.AuthorID = a.ID
WHERE a.Author Like "Arn*")
There are advantages in building more that one form, books as a main form and authors as a subform, then authors as a main form and books as a subform. It is often easier on the user.