The below is my code. A simple loop to go through records in a query (qryMasterImageFolders) and then call the sub fs.listImages...
However, I keep receiving the 3061 error and the below line is highlighted:
Set rst = qdf.OpenRecordset()
The query does contain records and I have checked the spelling - what am I missing?
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("qryMasterImageFolders")
Set rst = qdf.OpenRecordset()
With rst
Do Until .EOF
fs.listImages DLookup("ImageFolder", "qryMasterImageFolders")
.MoveNext
Loop
End With
rst.Close
Set rst = Nothing
Set qdf = Nothing
Set db = Nothing
I have changed my code and it is now working:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT ImageFolder FROM tmpImagePaths")
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
fs.listImages DLookup("ImageFolder", "qryMasterImageFolders")
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
What you should have done:
Set rs = db.OpenRecordset("qryMasterImageFolders")
Related
I am trying to set all Nulls in a table (tblIdea) to NS but the following code throws an 'Object Required' error and highlights the line starting with 'If.'
Sub CommitNS()
Dim db As dao.Database
Dim tdf As dao.TableDef
Dim fld As dao.Field
Set db = CurrentDb
Set tdf = db.TableDefs("tblIdea")
For Each fld In tdf.Fields
If fld Is Null Then
fld = "NS"
End If
Next fld
Set tdf = Nothing
Set db = Nothing
End Sub
You miss the actual update:
Sub CommitNS()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim Update As Boolean
Set db = CurrentDb
Set rst = db.OpenRecordset("Select * From tblIdea")
While Not rst.EOF
For Each fld In rst.Fields
If IsNull(fld.Value) Then
Update = True
Exit For
End If
Next
If Update = True Then
' Record needs update.
rst.Edit
For Each fld In rst.Fields
If IsNull(fld.Value) Then
fld.Value = "NS"
End If
Next
rst.Update
Update = False
End If
rst.MoveNext
Wend
rst.Close
Set fld = Nothing
Set rst = nothing
Set db = Nothing
End Sub
I'm trying to write This Programm:
Function funktion()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rt As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("All")
Do While Not rs.EOF
Set rk = db.OpenRecordset("Archive")
'here I want to copy(append) the current index(of Table"All") into the next free
index (of table "archive")
Do something
rs.MoveNext
Loop
my programm works well just I need to append the current row from "All" into the next free row from table "Archive".
Thank you for your help
here´s a example of how you can do this using ADO and not DAO to access data.... in example above.. i add all records to another table... but easily you can just add the current record in your loop...
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Set cnn = CurrentProject.Connection
Set rs = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
'open main recordset (TDetPed)
rs.Open "SELECT TDetPed.* FROM TDetPed WHERE CodPed = " & CodPed & "", cnn, adOpenKeyset, adLockOptimistic
'open clone table recordset (TDetPedTemp)
rs2.Open "TDetPedTemp", cnn, adOpenKeyset, adLockOptimistic
'move to first record of main table
rs.MoveFirst
'add record by record in clone table(rs2) from maintable(rs)
Do Until rs.EOF
rs2.AddNew
rs2("CodPed") = rs("CodPed")
rs2("CodDetPed") = rs("CodDetPed")
rs2("CodInterno") = rs("CodInterno")
rs2("DescrDetPed") = rs("DescrDetPed")
rs2("DescontoDetPed") = rs("DescontoDetPed")
rs2("CodProd") = rs("CodProd")
rs2("PreçoDetPed") = rs("PreçoDetPed")
rs2("QtdeDetPed") = rs("QtdeDetPed")
'update current added record in clone table
rs2.Update
'move to next record in main table
rs.MoveNext
'Move para o proximo registro do detalhe do pedido
Loop
'close everthing
rs.Close
rs2.Close
cnn.Close
'clean everthing
Set rs = Nothing
Set rs2 = Nothing
Set cnn = Nothing
Just completing the answer... a simple example using DAO (Data Access Objetcs) and not ADO(Active Data Objetcs). (more info https://msdn.microsoft.com/en-us/library/aa261340%28v=vs.60%29.aspx / http://www.utteraccess.com/wiki/index.php/Choosing_between_DAO_and_ADO)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = dbsNorthwind.OpenRecordset("Tbl1")
rs.AddNew
rs!Cidade = "Curitiba"
rs!Country = "Brazil"
'.... others fields
rs.Update
Private Sub Command22_Click()
' This section deals with grabbing the 3 calculations from qry_LiveOEE
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
'Open a dynaset-type Recordset using a saved query
Set rst = dbs.OpenRecordset("qry_LiveOEE", dbOpenDynaset)
rst.MoveFirst
numfg_posted = rst!SumOfqty_complete
numOEE = rst!OEE
numpp_lhr = rst!ACT_PPLHR
rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing
I get an error saying Too few parameters. Expected 4. This query has 5 things in the criteria section (design view), so why is it saying I need 4 parameters?
The 5 things in the criteria section (all under different fields) are:
input from a form
input from a form
Switch statement based on current time
Date()
Is Not Null
Try this (not tested) update to the code:
Private Sub Command22_Click()
' This section deals with grabbing the 3 calculations from qry_LiveOEE
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf AS DAO.QueryDef
Dim prm As DAO.Parameter
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_LiveOEE")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
rst.MoveFirst
numfg_posted = rst!SumOfqty_complete
numOEE = rst!OEE
numpp_lhr = rst!ACT_PPLHR
rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing
Make sure all the values in the parameters are available - i.e. the form is open.
I am unable to get the count of records by openining Ms Access Query, I use the following code.
Private Sub CmdGetData_Click()
Dim WRK As Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim StrSql As String
Set WRK = DBEngine.Workspaces(0)
Set db = CurrentDb
StrSql = "select * from [QrySalePatti]"
Set rs = db.OpenRecordset(StrSql, dbOpenDynaset)
Do While (Not rs.EOF)
rs.MoveFirst
rs.MoveLast
MsgBox rs.RecordCount
Loop
exitRoutine:
If Not (db Is Nothing) Then
db.Close
Set db = Nothing
End If
Set WRK = Nothing
End Sub
You should not need a Do While loop to get the RecordCount.
Set rs = db.OpenRecordset(StrSql, dbOpenDynaset)
With rs
If Not (.BOF And .EOF) Then
.MoveLast
End If
MsgBox .RecordCount
End With
However if your goal is only to count the rows from QrySalePatti, you could use a SELECT Count(*) query and read the value returned from that.
StrSql = "SELECT Count(*) AS row_count FROM [QrySalePatti]"
Set rs = db.OpenRecordset(StrSql)
MsgBox rs!row_count
Or you could use a DCount expression.
MsgBox DCount("*", "QrySalePatti")
I'm grabbing field row from one table and creating a new table for each row. The new tables will have names equal to the row they correspond to.
Here is my code:
Option Compare Database
Public Function createTables()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim fld As DAO.Field
Dim strSQL As String
strSQL = "Select SKUS from SKUS"
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
Set fld = rst.Fields("SKUS")
'MsgBox fld.Value
rst.MoveFirst
Do While Not rst.EOF
Set tdf = db.CreateTableDef(fld.Value)
Set fld = tdf.CreateField("SKUS", dbText, 30)
tdf.Fields.Append fld
Set fld = tdf.CreateField("Count", dbInteger)
tdf.Fields.Append fld
db.TableDefs.Append tdf
rst.MoveNext
Loop
End Function
The problem is that after the first iteration of the code (the first table is created), it gives me an error "Invalid operation" pointing to the line
...
Set tdf = db.CreateTableDef(fld.Value)
...
Why do you think this is? I have a feeling its because I need to re set fld or rst, but I'm not sure.
Can anyone help me out with this?
Thanks!
It doesn't seem like you're reading any new tuples from rst, thus the CreateTableDef will be called with the same value repeatedly. Try changing this:
[...]
Set fld = rst.Fields("SKUS")
rst.MoveFirst
Do While Not rst.EOF
Set tdf = db.CreateTableDef(fld.Value)
[...]
Into this:
[...]
rst.MoveFirst
Do While Not rst.EOF
Set fld = rst.Fields("SKUS")
Set tdf = db.CreateTableDef(fld.Value)
[...]
...if your intention is to create one table based on every tuple in the SKUS table.