Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gurvinder - 23 Dec 2005 03:17 GMT
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>
Perry - 30 Dec 2005 09:42 GMT
Try following:

Substitute Str() and Int() usage by resp. CStr() and CInt() functions in
buildup of variable 'strSQL'

Leave the rest of the code as is...
-------------------------------------
Krgrds,
Perry

>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.
[quoted text clipped - 84 lines]
>
> </snip>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.