Results 1 to 8 of 8
  1. #1
    dgj32784 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    22

    Filling Word Form Using Access Data and SQL Recordset

    Alright. Frankly this is over my head. I've tried to read-up on the web and have attempted to customize some code I found for the subject. I'm getting the following error message: "3061Too few parameters. Expected 1."



    I believe this means that for some reason txtSurveyID (a control on my Access form) is not being "passed" to the SQL. This code is definitely not clean and I've commented out some stuff that was in it from the original source not thinking I really need it. And there is still probably some stuff that is not needed. What am I missing here? Is my syntax for wrong in the SQL statement? The corresponding field in the Word form is obviously "wrdSurveyID".

    Code:
    Private Sub cmdGetFullSiteReport_Click()
    
    
        Dim DOC_PATH As String
        Dim DOC_NAME As String
        Dim appWord As Word.Application
        Dim doc As Word.Document
        Dim rst As DAO.Recordset
        Dim strSQL As String
        'Dim SurveyID As String
        
        DOC_PATH = "C:\aaa\projects\bmp_implementation_survey\2012\database\reports\"
        DOC_NAME = "20130109_bmp_imp_full_site_report_template.doc"
        'SurveyID = Me!txtSurveyID
        
        On Error Resume Next
    
    
        Set appWord = GetObject(, "Word.application")
            If Err = 429 Then
                Set appWord = New Word.Application
                Err = 0
            End If
        With appWord
            Set doc = .Documents(DOC_NAME)
                If Err = 0 Then
                    If MsgBox("Do you want to save the current document " _
                    & "before updating the data?", vbYesNo) = vbYes Then
                    .Dialogs(wdDialogFileSaveAs).Show
                    End If
                    doc.Close False
                End If
        
    On Error GoTo ErrorHandler
        
        Set doc = .Documents.Open(DOC_PATH & DOC_NAME, , True)
    
    
        If Not IsNull(Me!txtSurveyID) Then
            strSQL = "SELECT tblSurveySites.strSurveyID, tblSurveySites.dtmSurveyDate, tblSurveySites.dtmSurveyTime, tblSurveySites.strSurveyorsFullName, tblSurveySites.strXCoordinate, tblSurveySites.strYCoordinate, tblSurveySiteLocation.strCountyName, tblSurveySiteLocation.strFeatureName, tblSurveySiteLocation.strRoadName, tblSurveySiteLocation.strRiverBasin " & vbCrLf & _
            "FROM tblSurveySites INNER JOIN tblSurveySiteLocation ON tblSurveySites.strSurveyID = tblSurveySiteLocation.strSurveyID " & vbCrLf & _
            "WHERE (((tblSurveySites.strSurveyID)= [txtSurveyID]));"
            Set rst = CurrentDb.OpenRecordset(strSQL)
    
    
        'ElseIf Not rst.EOF Then
            'strReportsTo = Nz(rst.Fields(0).Value)
            'rst.Close
        End If
    
    
        With doc
            .FormFields("wrdSurveyID").Result = Nz(Me!strSurveyID)
        End With
    
    
        .Visible = True
        .Activate
    
    
    End With
    
    
        Set rst = Nothing
        Set doc = Nothing
        Set appWord = Nothing
        
    Exit Sub
    
    
    ErrorHandler:
    MsgBox Err & Err.Description
    
    
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Which line triggers the error?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I believe this means that for some reason txtSurveyID (a control on my Access form) is not being "passed" to the SQL.
    You are correct.
    If you add a line "Debug.Print strSQL" before the line "Set rst = CurrentDb.OpenRecordset(strSQL)", you can see in the immediate window if the SQL line is formed correctly.

    Your WHERE clause is
    Code:
     "WHERE (((tblSurveySites.strSurveyID)= [txtSurveyID]));"
    Try the debug statement first

    Then change the line to
    Code:
     "WHERE (((tblSurveySites.strSurveyID)= " & Me.txtSurveyID & "));"
    Check the debug statement


    Also, you shouldn't have (don't need) "vbCrLf" in the strSQL string.

  4. #4
    dgj32784 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    22

    Same and new errors

    Thanks for the feedback. With respect to identifying the line of code that triggers the error - pardon my ignorance - but how do I do that?

    I think I tried what you suggested John and it was still producing the "3061Too few paramerters. Expected 1." error message.

    Code:
    strSQL = "SELECT tblSurveySites.strSurveyID, tblSurveySites.dtmSurveyDate, tblSurveySites.dtmSurveyTime, tblSurveySites.strSurveyorsFullName, tblSurveySites.strXCoordinate, tblSurveySites.strYCoordinate, tblSurveySiteLocation.strCountyName, tblSurveySiteLocation.strFeatureName, tblSurveySiteLocation.strRoadName, tblSurveySiteLocation.strRiverBasin FROM tblSurveySites INNER JOIN tblSurveySiteLocation ON tblSurveySites.strSurveyID = tblSurveySiteLocation.strSurveyID WHERE (((tblSurveySites.strSurveyID)= " & Me.txtSurveyID & "));"
    I then remembered that in a few INSERT statements I used single and double quotes to refer to a control on a form. So I tried that.

    Code:
    strSQL = "SELECT tblSurveySites.strSurveyID, tblSurveySites.dtmSurveyDate, tblSurveySites.dtmSurveyTime, tblSurveySites.strSurveyorsFullName, tblSurveySites.strXCoordinate, tblSurveySites.strYCoordinate, tblSurveySiteLocation.strCountyName, tblSurveySiteLocation.strFeatureName, tblSurveySiteLocation.strRoadName, tblSurveySiteLocation.strRiverBasin FROM tblSurveySites INNER JOIN tblSurveySiteLocation ON tblSurveySites.strSurveyID = tblSurveySiteLocation.strSurveyID WHERE (((tblSurveySites.strSurveyID)= ' " & Me.txtSurveyID & " '));"
    This produce the following error message "2465Microsoft Office Access can't find the field 'strSurveyID' referred to in your expression."

    Not sure what that's all about. The field most definitely exists. Any guidance would be appreciated.

    Thanks,
    David

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Debugging: Read the first two paragraphs after the intro at Chip Pearson's site - http://www.cpearson.com/excel/DebuggingVBA.aspx
    (Don't use the STOP command in code)

    In the code, click anywhere in this line

    DOC_PATH = "C:\aaa\projects\bmp_implementation_survey\2012\da tabase\reports\"

    then press the "F9". Go back to the form and run the code. You should be dumped back into the IDE.
    Press the "F8" key to execute one line at a time.


    Does table "tblSurveySites" have a field named "strSurveyID"?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The apostrophe delimiter is need for criteria when field is text datatype. For date/time use # delimiter.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    dgj32784 is offline Novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    22

    Debug information

    Okay, looks like the error is occurring with this line:

    Code:
    .FormFields("wrdSurveyID").Result = Nz(Me!strSurveyID)
    And this would be where me fumbling in the dark starts to really become a factor. This was part of the code I've attempted to modify from various online sources. I forget what Nz means, but now reflecting on the error I guess it's because there is no "strSurveyID" on my form. What I need is for this word form field (wrdSurveyID) to be set equal to "strSurveyID" from the strSQL. How to I refer to that value?

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I now see several things to correct.
    Code:
        With doc
            .FormFields("wrdSurveyID").Result = Nz(Me!strSurveyID)
        End With
    "Me" is a shortcut to "Forms!FormName" and is used on a form. You cannot refer to a query in code using "ME". You have to use the recordset reference, in this case "rst"

    You are also missing part of the NZ() function. From Help:
    The Nz function is useful for expressions that may include Null values. To force an expression to evaluate to a non-Null value even when it contains a Null value, use the Nz function to return a zero, a zero-length string, or a custom return value.
    You are missing the "ValueIfNull" part.

    Code:
    .FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,ValueIfNull)
    "ValueIfNull" should be replaces with what you want to be returned if "strSurveyID" is NULL.

    For Instance, if "strSurveyID" is a Long Int, you might want to return a Zero. It would look like this
    Code:
    .FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,0)
    If "strSurveyID" is a string, you might want to return custom text. It would look like this:
    Code:
    .FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,"Not found. Please re-enter")
    You might try
    Code:
        With doc
            .FormFields("wrdSurveyID").Result = Nz(rst!strSurveyID,0)
        End With



    BUT, the main problem is you opened the recordset (rst) , and didn't check to see if there were records returned and handle the error before it gets to the "with DOC" statement

    Code:
            strSQL = "SELECT tblSurveySites.strSurveyID, tblSurveySites.dtmSurveyDate, tblSurveySites.dtmSurveyTime, tblSurveySites.strSurveyorsFullName, tblSurveySites.strXCoordinate, tblSurveySites.strYCoordinate, tblSurveySiteLocation.strCountyName, tblSurveySiteLocation.strFeatureName, tblSurveySiteLocation.strRoadName, tblSurveySiteLocation.strRiverBasin " & vbCrLf & _
            "FROM tblSurveySites INNER JOIN tblSurveySiteLocation ON tblSurveySites.strSurveyID = tblSurveySiteLocation.strSurveyID " & vbCrLf & _
            "WHERE (((tblSurveySites.strSurveyID)= [txtSurveyID]));"
            Set rst = CurrentDb.OpenRecordset(strSQL)
    'not check for records
     If rst.bof and rst.EOF then
        'do something
        ' maybe exit the sub???
    end if


    Or you could check to see if rst!strSurveyID is NULL and handle the error someway.
    You could do this:
    Code:
        With doc
            If IsNull(rst!strSurveyID) then
               'strSurveyID is NULL
               ' do something
            ELSE
            .FormFields("wrdSurveyID").Result = rst!strSurveyID
           End If
        End With
    (don't have to do both)


    Sorry this is so long....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Form data in SELECT statement recordset
    By nvrwrkn in forum Programming
    Replies: 15
    Last Post: 10-16-2012, 03:57 PM
  2. Import data from access to word
    By rcmjr86 in forum Import/Export Data
    Replies: 1
    Last Post: 10-05-2012, 07:57 AM
  3. Word 2007 filling the forms
    By thanosgr in forum Forms
    Replies: 1
    Last Post: 06-03-2012, 05:21 PM
  4. transferring data from word to access
    By RickScolaro in forum Access
    Replies: 6
    Last Post: 09-08-2011, 05:17 PM
  5. Trouble printing labels (data from Access) in Word
    By Austruck in forum Import/Export Data
    Replies: 2
    Last Post: 08-08-2011, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums