I know how to use access VBA to get the total count of records in the recordset. Which is what I do below, but I am in need of taking my main rs and splitting the rs into sub-recordsets every 50 records. Meaning if rs.RecordCount = 200 - then I would want to create, rs1, rs2, rs3, rs4 and the recordCount of each should be 50.
How can I dynamically split a recordset into sub-recordsets as desecribed above? This is how I get the count in the primary recordset
Set rs = CurrentDb.OpenRecordset("Select firen, daja, globaltwo", dbOpenDynaset)
With rs
If Not .EOF And Not .BOF Then
.MoveLast
.MoveFirst
Do While Not .EOF
qty = rst.RecordCount
Debug.Print qty
Loop
End If
End With
rs.Close
Set rs = Nothing
You might want to see if the OLEDB provider supports paging. See this https://support.microsoft.com/en-us/help/202125/how-to-page-through-a-recordset-from-asp
The given link to an Access 97 database which suggest access does support this even though the code is an Active Server Page the PageSize property is still available to VBA clients.
Related
I have an access table with below columns
Id process
1 Lotus
2
3
4 Rose
5
6
7
8 Lilly
9
10
I am looking for an update query, which can have fuctionality same line filldown in excel.
This will do what you want.
Private Sub Command1_Click()
Dim Rs As Recordset, strSQL As String, CurData As Variant
'Note: Set a reference to the Microsoft DAO Object library via Tools | References
'Amend this to indicate the table and sort by the autonumber id
strSQL = "SELECT * FROM YOUR_TABLE"
'Opens a recordset (like a query) to the table
Set Rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
'Loop through the records in the table and if the previous row didnt have data in the
'Repaired_By field then populate the previous Repaired_By data. Assumes the first record is
'populated with info
Rs.MoveLast
Rs.MoveFirst
Do
If Rs!CONTACT_ID = "" Or IsNull(Rs!CONTACT_ID) Then
'This field is blank so populate the info from the variable
Rs.Edit
Rs!CONTACT_ID.Value = CurData
Rs.Update
Else
'This field isnt blank so store the info in the variable in case the next record is blank
CurData = Rs!CONTACT_ID.Value
End If
Rs.MoveNext
Loop While Not Rs.EOF
'Remove the recordset object from memory
Rs.Close
Set Rs = Nothing
End Sub
I have some knowledge of Access and VBA programming, but I stress some. I haven't really used Access since college.
I have a table called Location with 2 fields: Postcode and a Group Code. This list has 1,693,353 records.
I have another table called Group with 2 fields: Group Name and Group Code.
There are about 300 different Authority Records.
I need to make a table/query for each Group and have the different location postcodes in the tables.
I know that I could go through and make 300 different queries all with the Group code as the criteria and that would match them up, but that means making 300 different queries.
What I would like to know is if there is a way of automating this process. I'm not asking for people to create it for me (unless they want to) but if there are any guides or tutorials that people could recommend for learning Access VBA that would help as well.
This query will contain all your groups (I've named it SQL_Group)
SELECT GroupName, Postcode FROM [Group] INNER JOIN Location ON Group.GroupCode = Location.GroupCode will create your groups.
This code will cycle through each group:
Public Sub FilterQuery()
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim rst_Groups As DAO.Recordset
Dim rst_Filtered As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set qdf = db.QueryDefs("SQL_Group")
Set rst = qdf.OpenRecordset
Set rst_Groups = db.OpenRecordset("Group")
With rst_Groups
If Not (.BOF And .EOF) Then
.MoveFirst
Do While Not .EOF
rst.Filter = "GroupName = '" & rst_Groups.Fields("GroupName") & "'"
Set rst_Filtered = rst.OpenRecordset
With rst_Filtered
If Not (.BOF And .EOF) Then
.MoveFirst
Do While Not .EOF
Debug.Print .Fields("GroupName") & " : " & .Fields("PostCode")
.MoveNext
Loop
End If
.Close
End With
.MoveNext
Loop
End If
.Close
End With
Set rst_Filtered = Nothing
Set rst = Nothing
Set rst_Groups = Nothing
End Sub
I am combining multiple Excel Worksheets into one SharePoint list so our data is all in one place and modifiable by multiple users at once. The Append query worked without a hitch.
Now I am trying to update one filed in the list with an update query but it keeps locking up MS Access (Not Responding, 100% CPU usage). I have to terminate from the task manager.
I have let it run for as much as 10 minutes. So then I switched to the one time use sub procedure below to update through a recordset. Same issue.
I am able to update the field manually one at a time via the linked list in MS Access. I can update the field via datasheet and dialog in SharePoint.
SharePoint 2010
MS Access 2013
Does anyone have any ideas?
Option Compare Database
Option Explicit
Public Sub UpdateDataPlateDates()
On Error GoTo err_trap
Dim db As DAO.Database: Set db = CurrentDb()
Dim rst As DAO.Recordset
Dim strSQL As String
Dim i As Integer: i = 1
Dim vDate As Variant
Dim sNum As String
strSQL = "SELECT TML.[SERIAL NUMBER], TML.[DATA PLATE DATE] FROM [Tool Master List] AS TML WHERE (((TML.[DATA PLATE DATE]) Is Null));"
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
With rst
If Not (.BOF And .EOF) Then
.MoveLast: .MoveFirst
Do Until .EOF
sNum = ![SERIAL NUMBER].Value
vDate = DLookup("[ACCEPT DATE]", "Tool information", "[SERIAL NUMBER]='" & sNum & "'")
Debug.Print i, sNum, vDate
If Not (IsNull(vDate) Or IsEmpty(vDate)) Then
vDate = CDate(vDate)
.Edit
![DATA Plate Date] = vDate '//FAILS-LOCKS UP RIGHT HERE WITHOUT and ERROR
.Update
End If
.MoveNext
i = i + 1
sNum = vbNullString
vDate = Null
DoEvents
Loop
End If
.Close
End With
Set rst = Nothing
Set db = Nothing
exit_sub:
Exit Sub
err_trap:
Debug.Print Err.Number, Err.Description
Stop
Resume
End Sub
Is it possible the item you are trying to update is being edited by a user? Have you considered linking the SharePoint table and performing an update query instead?
I have a query that has CustID with mutiple Business affiliated with the CustID. I can't use Dlookup because it only returns one variable. I want to show on a form that for this custID, here are all the businesses it's affiliated it. I want the Businesses to show up into a field (business) in another table on the form.
I started out by this
Public Sub OpenRecordset()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Q:businesses")
Do While Not rs.EOF
T:Custinfo!business = NAME (I am lost in between on how to identify the custid and place the businesses into the table field as a Dlookup)
rs.movenext
Loop
rs.Close
Set rs = Nothing
db.Close
End Sub
I keep looking at other examples but can't seem to tie together where the dlookup replacement will take place and how will you have to put this on a form as a datasheet?
You don't need a DLookup. You could do one of two things:
1) Use a listbox and set the recordsource equal to your query (assuming Q:businesses has been appropriately defined to give the businesses as a result)
2) Still need your query to be appropriate, but you could create a string with all of the businesses in it:
Public Sub OpenRecordset()
Dim db As Database
Dim rs As Recordset
Dim StrBusinesses As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qryBusinesses")
If rs.EOF and rs.BOF Then
MsgBox("No businesses exist for this Customer")
Exit Sub 'Or do whatever else you want if there are no matches
Else
rs.MoveFirst
End If
StrBusinesses = ""
Do While Not rs.EOF
StrBusinesses = StrBusinesses & rs!Business & ", "
rs.movenext
Loop
rs.Close
StrBusinesses = Left(StrBusinesses, Len(StrBusinesses) - 2)
Forms!MyForm.MyField = StrBusinesses 'Set the field equal to the string here
Set rs = Nothing
db.Close
End Sub
Of course this assumes that the query "Q:Business" is defined to get the appropriate info such as:
SELECT custID, business FROM tblBusinesses WHERE custID = X
where "X" is the custID you are looking for.
If you need to set the query dynamically, you will need to set a querydef.
EDIT to include querydef code***********************
Also changed the name of the query to "qryBusinesses" in the code above and below as I'm not sure whether you can make a query with a colon in it.
To set the querydef, put this at the beginning of the code:
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("qryBusinesses")
qdf.SQL = "SELECT custID, business FROM tblBusinesses" _
& " WHERE custID = " & Forms!MyForm.CustID 'replace with actual form and field
This assumes that i) qryBusinesses exists already,
ii) custID is a number field
EDIT**************
If you define the query to look at the form itself, you would not need to set the sql, so if the query were defined (either in VBA or through the query wizard) as:
qdf.sql = "SELECT custID, business FROM tblBusinesses" _
& " WHERE custID = Forms!MyForm.CustID"
then you would not need to redefine the sql. However, it is a bit more dynamic to put the custID into the qdf itself as it is easier to debug any issues as you can see the exact sql that is being run in the original method.
I'm trying to get the record count of a table, and if count is greater than 17, create a new table.
Dim rst As DAO.Recordset
strSQL = "Select * from SKUS"
Set rst = db.OpenRecordset(strSQL)
If rst.RecordCount > 17 Then
Set tdf = db.CreateTableDef("161-0363")
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
End If
This code doesn't create a new table, but when I change the if statement to this, it works:
...
If rst.RecordCount > 0 Then
Set tdf = db.CreateTableDef("161-0363")
...
So the RecordCount is returning 1, I think. Why is this happening? I know for sure the table has 18 rows in it.
Can anyone help me out?
You have to force the recordset to move through all the rows to get an accurate count. Try this:
...
Set rst = db.OpenRecordset(strSQL)
rst.MoveLast
If rst.RecordCount > 17 Then
...
Unless you are doing something else with the recordset that you're not showing in your snippet, you can simplify your code by just doing a check of the record count using the domain count function:
If DCount("*", "SKUS") > 17 Then
If you plan to loop through the recordset after doing the RecordCount check, make sure you move back to the first record before you start your loop:
rst.MoveFirst
The problem with strsql is that when the string represents a parameter query the above code does not work.
In this case I would use a meter in the code as simplified below:
rs.movelast
X = rs.recordcount
Rs.movefirst
Do until rs.eof
For i = 1 to X
If i<=17 then
Do things
Else
Do things
End if
Next i
Loop
You need to move through the recordset to get an accurate count of records in DAO. Since the recordset is only used to get a count of the records, you can just do rst.MoveLast and then the rst.RecordCount will be accurate.
Depending on the type of recordset being used (ex: forward only) the rst.MoveFirst line suggested by others above may not always work. If you're not touring through all the records individually, just do the .MoveLast without the .MoveFirst.
...
Set rst = db.OpenRecordset(strSQL)
rst.MoveLast
If rst.RecordCount > 17 Then
...