Run-Time error '3061': executing SQL query using DAO

G

Gurvinder

I am picking up VBA after 6 years and need help with running a SQL
query from Word to get data from Access MDB file.

Error:
Run-Time error '3061':
Too few parameters. Expected 1

' Debug Window at the time of the error
debug.Print strsql
SELECT [Oral_Communication] FROM MidYearComments WHERE ([Grade]=1) AND
([GradingCode]='E');

VBA Code in Word Docment
<snip>
Public Function GetData(ByVal tblName As String, ByVal fldName As
String, ByVal NumGrade As Integer, ByVal StrGradingCode As String) As
String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim NoOfRecords As Long

' Numbers in Grade in Access range from
' -1 for JKG and 0 for KG
' followed by 1 - 12 for grades.

If Str(NumGrade) = "JKG" Then
NumGrade = -1
ElseIf Str(NumGrade) = "KG" Then
NumGrade = 0
End If

' Open the database
Set db = OpenDatabase("C:\Reports\Comments.mdb")
Dim strSQL As String

' Retrieve the recordset
' This one works fine
' Set rs = db.OpenRecordset("SELECT [Oral_Communication] FROM " _
' & "MidYearComments")

' Retrieve the recordset
' This one generates the error
strSQL = "SELECT [" & fldName & "] FROM " & tblName & " " _
& "WHERE ([Grade]=" & Int(NumGrade) & ") AND " _
& "([GradingCode]='" & StrGradingCode & "');"
Set rs = db.OpenRecordset(strSQL) , dbOpenSnapshot)

' Determine the number of retrieved records
With rs

.MoveLast

NoOfRecords = .RecordCount

.MoveFirst

End With

' Set the number of Columns = number of Fields in recordset

ListBox1.ColumnCount = rs.Fields.Count

' Load the ListBox with the retrieved records

ListBox1.Column = rs.GetRows(NoOfRecords)

' Cleanup

'rs.Close

db.Close

Set rs = Nothing

Set db = Nothing

' Not the value I want but I am using a sample value
' I want the value of the field in the recordset
GetData = Str(ListBox1.ColumnCount)
End Function

Private Sub Oral_Communication_FinalYear_Grade_Change()
strcomments = GetData("MidYearComments", "Oral_Communication",
Grade_ComboBox.Value, Oral_Communication_FinalYear_Grade.Value)
Me.Oral_Communication_Comments_TextBox1.Value = strcomments
End Sub

</snip>
 
T

TC

Three things.

(1) "Too few paamjeters: expected 1" usually means there is a syntax
error in the SQL statement, or the statement refers to tables or fields
that do not exist.

(2) The sql that you debug.print'ed looks syntactically correct to me.
Are you sure that all of the table & field names in that staement, are
correct?

(3) The following statement would not compile. It could not possibly
run, at all. So clearly you have not cut & pasted the code verbatim:

Set rs = db.OpenRecordset(strSQL) , dbOpenSnapshot)
^^^ illegal
extra closing bracket.

HTH,
TC
 

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