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>
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>