Question : add sequential number to field for query dataset

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
   
           

Answer : add sequential number to field for query dataset

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.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
[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
Random Solutions  
 
programming4us programming4us