Recordsetclone loop - ms-access

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

Related

MS Access Recordset Moving on Search even if no match found?

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

Access Form VBA: double record when saving

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

Access - Duplicate Record button creates a blank record

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?

Check a filtered table for specific data

I have a form (frm_Property) that shows information for a property and within that form another form (frm_Rate) that is a filtered table that displays all rates associated with that property. I want to check the filtered rate table to make sure that there are either no rates entered or there is a current rate (ie the current date falls between the start and end date of a rate). If there is no current rate (only with other rates entered) I want to stop the close of the form and a message box to open telling the user to enter a current rate.
After searching and complying many different things, this is the code that I have currently.
Private Sub Form_Unload(Cancel As Integer)
Dim rs As DAO.Recordset
Dim currdate, check As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Rate WHERE Property = " & Me.Property)
currdate = Date
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
'Check if rate is current rate
If currdate >= [tbl_rate.Start_Date] And currdat <= [tbl_rate.End_Date] Then
check = "Good"
Exit Do
Else
'Move to the next record.
check = "Bad"
rs.MoveNext
End If
Loop
MsgBox check 'testing to see if correctly identified if current rate exists
Else
MsgBox "There are no records in the recordset."
End If
If check = "Bad" Then
MsgBox "Please enter current rate."
Cancel = True
End If
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
End Sub
Currently it can detect if no record is entered, but it always tells me that there is no current rate even if there is and I am not sure why.
I also have this code located within frm_rate. I don't know if that is why I am having difficulty stopping the form from closing. I was reading about on unload stopping the form from closing which is why I chose that one. I suspect I am having trouble because my "On Unload" is in the subform.
Any insight would be much appreciated.
Unload event for subform occurs after Unload of main form, so the code in subform is useless. In main form you need to save the data first Me.Dirty = false After this your code will be able to see new data in table. Also you can use RecordsetClone property of subform for accessing the data in in subform.
Use the RecordsetClone of the subform and FindFirst:
Dim currdate As String
Set rs = Me!NameOfYourSubformCONTROL.Form.RecordsetClone
' Check to see if the recordset actually contains rows.
If rs.RecordCount = 0 Then
check = "Empty"
Else
' Check if rate is current rate.
currdate = Format(Date, "yyyy\/mm\/dd"
rs.FindFirst "#" & currdate "# Between tbl_rate.Start_Date And tbl_rate.End_Date"
If Not rs.NoMatch Then
check = "Good"
Else
check = "Bad"
End If
End If

Access: Move to next record until EOF

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