Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

add sequential number to field for query dataset

Posted on 2010-08-23
5
Medium Priority
?
927 Views
Last Modified: 2012-05-10
HI experts.  access 2007.  I've got a set of queries which all feed off one seed query.  the seed query has parameters set from a form.  I'm trying to add a sequential number to each of the set of queries using ado.  my tables are all on sql server.  problem is when i try to loop through the records using the query names I get "too few parameters expected 3" error.  code all works sweet if i change the sql to tables so the issue is the query parameters fed off the form - there are 3.  I've tried using querydefs to pass the parameters but keep getting no records - the code quits out on the if eof then close line before looping through records.  I've hard coded the query and parameters to ensure they are not the problem at this stage.  if i mouse over qdf line as i step through code i get qdf = nothing.  if i run this query with those parameters i get 2,500 records.  am i doing something obviously wrong?  is there a different solution? any help appreciated.  

here is code with querydefs and hardcoded query name and parameters

            Dim db As DAO.Database
            Dim qdf As DAO.QueryDef
            Dim prm As DAO.Parameter
            Dim rs As DAO.Recordset
            Dim strSql As String
            Dim strWhere As String
            Dim dblCounter As Double
            Dim strFa As String
       
            dblCounter = Me.txtFaStart 'number to start with
            'strWhere = Me.cboAssetSubGroup 'this feeds the rest of the sql string
            'strSql = "SELECT " & strWhere & ";"
           
            Set db = CurrentDb
            Set qdf = db.QueryDefs("qryRdsValRbs")
            qdf.Parameters(0).Value = "2010"
            qdf.Parameters(1).Value = "*"
            qdf.Parameters(2).Value = Null
            Set rs = qdf.OpenRecordset(dbOpenDynaset)
           
            DoCmd.SetWarnings False
'            Set db = CurrentDb
'            Set rs = db.OpenRecordset(strSql, dbOpenDynaset, dbSeeChanges)
           
            'loop through records increasing number by one
            With rs
                Do While Not .EOF
                    .Edit
                    strFa = "FA" & dblCounter
                    !afanId = strFa
                    dblCounter = dblCounter + 1
                    .Update
                    .MoveNext
                Loop
                .Close
            End With
       
            Set rs = Nothing
   
           
0
Comment
Question by:craigdnc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 85
ID: 33499436
"Too Few Parameters" generally means that you've got a Field or Control misspelled. Check the spelling of all field/control names in your code.
0
 

Author Comment

by:craigdnc
ID: 33500076
I originally had 5 but fixed 2 through method you suggested.  I suspect the other 3 are those that the seed query is looking for from the form which i have hardcoded in the posted code.  it seems that my problem is with the line Set qdf = db.QueryDefs("qryRdsValRbs") as this returns null if i hover over in step through.  The query name is correct. Will double check though. thanks
0
 
LVL 31

Accepted Solution

by:
Helen Feddema earned 2000 total points
ID: 33502082
You might want to use an alternate method, creating a filtered query or SQL statement as in the following code.  I use this method to prevent errors with queries that get values from forms.

[sample code fragment using the procedure]

   Dim dbs As DAO.Database
   Dim lngCount As Long
   Dim lngID As Long
   Dim rpt As Access.Report
   Dim rst As DAO.Recordset
   Dim strPrompt As String
   Dim strQuery As String
   Dim strRecordSource As String
   Dim strReport As String
   Dim strSQL As String
   Dim strTitle As String
   
   strRecordSource = "tblInventoryItemsComponents"
   strQuery = "qryTemp"
   Set dbs = CurrentDb

   'Numeric filter
   lngID = Nz(Me![ID])
   If lngID <> 0 Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[ID] = " & lngID & ";"
   End If

   'String filter
   strInventoryCode = Nz(Me![InventoryCode])
   If strInventoryCode <> "" Then
      strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
         & "[InventoryCode] = " & Chr$(39) & strInventoryCode & Chr$(39) & ";"
   End If

   'Date range filter from custom database properties
   dteFromDate = CDate(GetProperty("FromDate", ""))
   dteToDate = CDate(GetProperty("ToDate", ""))
   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   'Date range filter from controls
   If IsDate(Me![txtFromDate].Value) = True Then
      dteFromDate = CDate(Me![txtFromDate].Value)
   End If

   If IsDate(Me![txtToDate].Value) = True Then
      dteToDate = CDate(Me![txtToDate].Value)
   End If

   strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
      & "[dteDateReceived] Between " & Chr(35) & dteFromDate _
      & Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"

   Debug.Print "SQL for " & strQuery & ": " & strSQL
   lngCount = CreateAndTestQuery(strQuery, strSQL)
   Debug.Print "No. of items found: " & lngCount
   If lngCount = 0 Then
      strPrompt = "No records found; canceling"
      strTitle = "Canceling"
      MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
      GoTo ErrorHandlerExit
   Else
      'Use this line if you need a recordset
      Set rst = dbs.OpenRecordset(strQuery)
   End If

   'Use SQL string as the record source of a form
   strFormName = "fpriLoadSoldPackingSlip"
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acDesign
   Set frm = Forms(strFormName)
   frm.RecordSource = strSQL
   DoCmd.OpenForm FormName:=strFormName, _
      view:=acNormal
   
   'Use SQL string as the record source of a report
   strReport = "rptLoadSold"
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewDesign, _
      windowmode:=acHidden
   Set rpt = Reports(strReport)
   rpt.RecordSource = strSQL
   DoCmd.OpenReport ReportName:=strReport, _
      view:=acViewNormal, _
      windowmode:=acWindowNormal
   'DoCmd.Save objecttype:=acReport, objectname:=strReport
   'DoCmd.Close objecttype:=acReport, _
      objectname:=strReport

=========================

Public Function CreateAndTestQuery(strTestQuery As String, _
   strTestSQL As String) As Long
'Created by Helen Feddema 28-Jul-2002
'Last modified 6-Dec-2009

On Error Resume Next
   
   Dim qdf As DAO.QueryDef
   
   'Delete old query
   Set dbs = CurrentDb
   dbs.QueryDefs.Delete strTestQuery

On Error GoTo ErrorHandler
   
   'Create new query
   Set qdf = dbs.CreateQueryDef(strTestQuery, strTestSQL)
   
   'Test whether there are any records
   Set rst = dbs.OpenRecordset(strTestQuery)
   With rst
      .MoveFirst
      .MoveLast
      CreateAndTestQuery = .RecordCount
   End With
   
ErrorHandlerExit:
   Exit Function

ErrorHandler:
   If Err.Number = 3021 Then
      CreateAndTestQuery = 0
      Resume ErrorHandlerExit
   Else
   MsgBox "Error No: " & Err.Number _
      & " in CreateAndTestQuery procedure; " _
      & "Description: " & Err.Description
   End If
   
End Function

Open in new window

0
 

Author Closing Comment

by:craigdnc
ID: 33539915
Helen. not sure why qry defs didn't work as documented in this case.  thanks for workaround.
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 33542086
There are always severals ways to do any particular task in Access, and sometimes you just have to try and see which one works in a given situation.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question