I need to loop through a form by moving to the next record in the recordset.
I am using the Form_Current event to loop thru.
I have used a couple of statements and have different outcomes.
This one sometimes crashes and gives the error message: "You can't go to the specified record."
DoCmd.GoToRecord , , acNext
This one only goes upto 72 records and stops.
DoCmd.RunCommand acCmdRecordsGoToNext
This one only goes upto 129 records and stops.
Me.Recordset.MoveNext
Trying to find an instruction that will go to the next record untill it reaches the End of File.
I am using Access 2010 (Access 2002 -2003 file format mdb) as the front end. The recordsource is a SQL Server 2008 linked View.
To loop from current record to the end:
While Me.CurrentRecord < Me.Recordset.RecordCount
' ... do something to current record
' ...
DoCmd.GoToRecord Record:=acNext
Wend
To check if it is possible to go to next record:
If Me.CurrentRecord < Me.Recordset.RecordCount Then
' ...
End If
If (Not IsNull(Me.id.Value)) Then
DoCmd.GoToRecord , , acNext
End If
Hi,
you need to put this in form activate, and have an id field named id...
this way it passes until it reaches the one without id (AKA new one)...
I have done this in the past, and have always used this:
With Me.RecordsetClone
.MoveFirst
Do Until .EOF
If Me.Dirty Then
Me.Dirty = False
End If
.MoveNext
Me.Bookmark = .Bookmark
Loop
End With
Some people would use the form's Recordset, which doesn't require setting the bookmark (i.e., navigating the form's Recordset navigates the form's edit buffer automatically, so the user sees the move immediately), but I prefer the indirection of the RecordsetClone.
Set rs = me.RecordsetClone
rs.Bookmark = me.Bookmark
Do
rs.movenext
Loop until rs.eof
If you want cmd buttons that loop through the form's records, try adding this code to your cmdNext_Click and cmdPrevious_Click VBA.
I have found it works well and copes with BOF / EOF issues:
On Error Resume Next
DoCmd.GoToRecord , , acNext
On Error Goto 0
On Error Resume Next
DoCmd.GoToRecord , , acPrevious
On Error Goto 0
Good luck! PT
Keeping the code simple is always my advice:
If IsNull(Me.Id) = True Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acLast
End If
Add This Code on Form Close Event whether you add new record or delete, it will recreate the Primary Keys from 1 to Last record.This code will not disturb other columns of table.
Sub updatePrimaryKeysOnFormClose()
Dim i, rcount As Integer
'Declare some object variables
Dim dbLib As Database
Dim rsTable1 As Recordset
'Set dbLib to the current database (i.e. LIBRARY)
Set dbLib = CurrentDb
'Open a recordset object for the Table1 table
Set rsTable1 = dbLib.OpenRecordset("Table1")
rcount = rsTable1.RecordCount
'== Add New Record ============================
For i = 1 To rcount
With rsTable1
rsTable1.Edit
rsTable1.Fields(0) = i
rsTable1.Update
'-- Go to Next Record ---
rsTable1.MoveNext
End With
Next
Set rsTable1 = rsTable1
End Sub
Related
This is driving me nuts. I have an access VBA form where I want to allow users to type a document number into a box in order to jump directly to that record.
The underlying table as a field "DocNum", and the number is always sequential. I want the searchbox to display the current Doc Number the user is on, if they type in a different number and hit enter, it should either jump to that record if it exists, or if it doesn't exist, stay on the current record.
I'm trying to accomplish thisby having a hidden textbox bound to "DocNum", and an unbound visible box on top of it. On Form_Current() the unbound box is made to match the underlying field. After, update I run the following code to perform the search.
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
With rs
rs.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If rs.NoMatch Then
MsgBox "Record not found."
GoTo Cleanup
Else
Me.Bookmark = rs.Bookmark
Exit Sub
End If
End With
Cleanup:
rs.Close
Set rs = Nothing
Set dbs = Nothing
End Sub
What's driving me insane is that even when it doesn't find a match.... it skips to the next record anyway. No matter what I do... move last, trying to bookmark before I even search, etc... I can't get it to not jump forward. What am I missing?
Try simplifying it:
Private Sub txt_DocNumSearch_AfterUpdate()
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
With rs
.FindFirst "[DocNum] = " & Str(Me![txt_DocNumSearch])
If .NoMatch Then
MsgBox "Record not found."
Else
Me.Bookmark = .Bookmark
End If
.Close
End With
End Sub
I have two forms: ‘frmClient’, (which has a subform that lists applicants), and ‘frmDisclosure’, which shows details of applicants. On frmClient there is a command button that opens a specified record in frmDisclosure. The procedure is Private Sub Command10_Click() - see below. This works.
The problem is that once in frmDisclosure via frmClient, it is not possible to move to another record. The procedure for opening another record in frmDiscloure is in a combobox control: Private Sub ComboFind_AfterUpdate().
This normally works, but it never works if frmDiscloure has been opened via frmClient. I have tried ‘requery’ and ‘refresh’ in various situations, and have tried closing frmClient once frmDisclosure is open. None of this works. If I want to get to a different record, the only solution I have at present is to close frmDisclosure and reopen it.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub Command10_Click()
If NumForms > 0 Then
DoCmd.OpenForm "frmDisclosure"
Forms!frmDisclosure.FilterOn = False
DoCmd.OpenForm "frmDisclosure", acNormal, "", "[DiscPK]=" & Me.DiscPK, , acNormal
Else
DisplayMessage ("No form ref for this application.")
Exit Sub
End If
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Sub ComboFind_AfterUpdate()
Dim rs As Object
Set rs = Me.RecordsetClone
rs.FindFirst "[DiscPK] = " & Str(Nz(Me![ComboFind], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
frmDisclosure is opened to a single record, there are no other records to navigate. The RecordsetClone has only one record, so of course code won't find any others. Turn off the filter first:
Private Sub ComboFind_AfterUpdate()
Me.FilterOn = False
With Me.RecordsetClone
.FindFirst "[DiscPK] = " & Nz(Me.ComboFind, 0)
If Not .NoMatch Then Me.Bookmark = .Bookmark
End With
End Sub
As you can see, declaring and setting a recordset object variable is not required. .EOF would probably work just as well, I have just always used NoMatch. This will set focus to record, not filter the form.
If you prefer to display single record, then set the Filter property.
Private Sub ComboFind_AfterUpdate()
Me.Filter = "DiscPK=" & Nz(Me.ComboFind, 0)
End Sub
In my Access Form, I have a Save button whose aim is to add the new record to the table called Data Processing List.
The problem is that pushing the button, the record entered is saved into the table twice. I mean, two identical records.
I can't understand why, because the code is really simple:
Private Sub Save_Click()
On Error GoTo Save_Click_Err
Me.Today.SetFocus
On Error Resume Next
DoCmd.RunCommand acCmdSaveRecord
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
With CurrentDb.OpenRecordset("Data Processing List")
.AddNew
!Department = Me.Department.Value
.Update
End With
Save_Click_Exit:
Exit Sub
Save_Click_Err:
MsgBox Error$
Resume Save_Click_Exit
End Sub
This is an example when I try to save the record with the word "prova".
You are saving twice:
DoCmd.RunCommand **acCmdSaveRecord**
...
With CurrentDb.OpenRecordset("Data Processing List")
.AddNew
!Department = Me.Department.Value
.**Update**
End With
I've got a fairly simple database, 5000 records about 60-70 fields, and I created a button to copy the current record and blank out some of the fields (which we called EXPAND).
I recently received a requests to do the same thing without blanking out any fields but it doesn't work. I used the Command Button wizard on my form and chose the Duplicate option, but the new record is completely blank. Additionally, I get an error message when I close the record that talks about "A large amount of data copied to the clipboard". I tried the original button I had made (EXPAND) and it resulted in the same issue. Searching back through old records, I see that it was working as recently as 6/10/2016 (10 days ago).
Has something changed which would prevent this code from executing properly? Is there a new setting/option I need to enable to get it working again? Or is there some alternative method I can use to accomplish the same result?
Here is the (very simple) code the system created to duplicate the record (which doesn't work):
Private Sub cmdDuplicate_Click()
On Error GoTo Err_cmdDuplicate_Click
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
DoCmd.RunCommand acCmdRecordsGoToNew
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdPaste
Exit_cmdDuplicate_Click:
Exit Sub
Err_cmdDuplicate_Click:
MsgBox Err.Description
Resume Exit_cmdDuplicate_Click
End Sub
The fastest and simplest way is to use DAO and the RecordsetClone of the form:
Private Sub cmdDuplicate_Click()
Dim rstSource As DAO.Recordset
Dim rstInsert As DAO.Recordset
Dim fld As DAO.Field
If Me.NewRecord = True Then Exit Sub
Set rstInsert = Me.RecordsetClone
Set rstSource = rstInsert.Clone
With rstSource
If .RecordCount > 0 Then
' Go to the current record.
.Bookmark = Me.Bookmark
With rstInsert
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "SomeFieldToPreset"
rstInsert.Fields(.Name).Value = SomeValue
ElseIf .Name = "SomeFieldToExclude
' Leave blank
Else
' All other fields.
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
End If
.Close
End With
Set rstInsert = Nothing
Set rstSource = Nothing
End Sub
I think the easiest way may to create an append query. Set the criteria field as the ID of the current record. This can be done pretty easily in the query design window.
No error message?
Do you have a Primary Key ID field on the form that doesn't allow you to copy duplicate ID?
The code below works BUT only to the first record in the continuous form its not looping threw the records. So if the first record is checked then I get the message "You need to select a RELEASE check box before proceeding." and if its not I get the other message.
What I need it to do is run down all the check boxes which is "ReleaseProduct" and exit sub at the first one it reaches if there is one and give a message and and if there isn't any check boxes checked run the Cancel = fncRequiredReleaseSelectedEmail(Me) and exit sub. Im messing up the loop somewhere...
Here is the code I have in my forms button....
Dim Cancel As Integer
Dim rs As Recordset
Set rs = Me.frmsub_ProductHoldData.Form.RecordsetClone
With rs
.MoveFirst
Do While Not .EOF
If rs.Fields("ReleaseProduct") = False Then
MsgBox "You need to select a RELEASE check box before proceeding.", vbInformation, "Selection Error"
Exit Sub
Else
Cancel = fncRequiredReleaseSelectedEmail(Me)
Exit Sub
End If
.MoveNext
Loop
End With
I'm having a hard time telling if this is exactly what you are looking for or not but this will look through all those records and only take action if one is checked.
The warning about selecting a Release check box only shows up if no boxes are checked.
Dim Cancel As Integer
Dim rs As Recordset
Set rs = Me.frmsub_ProductHoldData.Form.RecordsetClone
With rs
.MoveFirst
Do While Not .EOF
If rs.Fields("ReleaseProduct") = True Then
Cancel = fncRequiredReleaseSelectedEmail(Me)
Exit Sub
End If
.MoveNext
Loop
MsgBox "You need to select a RELEASE check box before proceeding.", vbInformation, "Selection Error"
End With