I am trying to go to the record which was just appended on a split form after requerying the form. the below code does not produce an error but it doesn't go to the new record either. All the parameters are correct. Any thoughts?
Forms!frmAddNewComponent.Requery
FindRecord Forms!frmAddNewComponent, Forms!frmAddNewComponent!txtID.Value, "ID"
Public Sub FindRecord(Component As Form, PK As Long, PKField As String)
With Component.RecordsetClone
.FindFirst PKField & "=" & PK
If .NoMatch Then
MsgBox "Record not found!", vbCritical
Else
Component.Bookmark = .Bookmark
End If
End With
End Sub
Related
I am opening a subform from my main form to allow data to be changed. Once the changes are made, I want to pass the data back to the main form. remove the filter, and go back to the original record. I have the Primary Key in the subform so I am passing it back. I used some code from another user's but it did not work not is my code. Any thoughts?
Private Sub cmd_close_Click()
Dim result As String
Dim ID As Variant
result = MsgBox("Save Geo Location?", vbOKCancel, "Save Geo Location")
If result = vbOK Then
Forms!frm_acct_select!GeoLoc_X = Me.txt_GeoLocX
Forms!frm_acct_select!GeoLoc_Y = Me.txt_GeoLocY
Forms!frm_acct_select.FilterOn = False
'this code fails immediately
With frm_acct_select.Form
ID = Me.txt_ParentID.Value
.FilterOn = False
.Recordset.FindFirst "ParentAccountID=" & ID
End With
'this code fails type mismatch criteria at the recordset.findfirst line
' With Forms!frm_acct_select
' ID = Me.txt_ParentID.Value
' .FilterOn = False
' .Recordset.FindFirst "ParentAccountID = " & ID
' End With
DoCmd.Close acForm, "sfrm_geoloc_update", acSaveNo
Else
DoCmd.Close acForm, "sfrm_acct_select_search", acSaveNo
End If
End Sub
Apparently, I still need to work on my string formatting.
Declaring ID as String was correct however I needed to add quotes to my code.
.Recordset.FindFirst "ParentAccountID = '" & ID & "'"
i have searched around and cant find an answer, so i am using Microsoft Access Office 2019 and cant seem to validate my textbox for a duplicate entry. The user will add a record and enter a short text primary key (which in this case is the new employee's ID) Field - [EMPID] in the [EMPDETAILS] table. The below code has worked for validating my Autonumber primary key but doesnt seem to work for a custom short text primary key and i am getting this error
"The expression you entered as a query parameter produced this error :
'VS123'
<= this is the Employee ID which is a custom short text primary key :
Private Sub unqidd_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Dim strMessage As String
'PartNum is the name of a textbox that contains the primary key, the rest are text to display.
strMessage = "Employee ID" & Me!unqidd & " already exists."
' confirm that part number doesn't already exist.
If (DLookup("[empid]", "[empdetails]", "[empID] = " & Forms![driverdetails]![unqidd])) Then
MsgBox strMessage, vbInformation, "Invalid Operation"
Cancel = True
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Exit_Here
End Sub
Since you are trying to add a string into [empID] I am going to assume it is a string field, in which case you need to include quotes:
If (DLookup("[empid]", "[empdetails]", "[empID] = '" & Forms![driverdetails]![unqidd] & "'")) Then
The solution is as below :
1) Create an unbound textbox (Text1616) and set its control source as
=DLookUp("[empid]","[empdetails]","[empID] = '" & [Forms]![Driverdetails]![unqidd] & "'")
2) The textbox (Unqidd) that will need to be validated, on its beforeupdate property set the event procedure code as below :
Private Sub unqidd_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Handler
Dim strMessage As String
'PartNum is the name of a textbox that contains the primary key, the rest are text to
display.
strMessage = "Employee ID" & Me!unqidd & " already exists."
' confirm that part number doesn't already exist.
If Me.unqidd.Value = Me.Text1616 Then
MsgBox strMessage, vbInformation, "Invalid Operation"
Cancel = True
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description
Resume Exit_Here
End Sub
'References :
'Table Name : empdetails
'Table primary key : empid (Short text)
'Form Name : Driverdetails
'Textbox 1 : unqidd (no formats, control source : empid)
'Textbox 2 : Text1616 (Unbound Textbox)
I have use "E "0000000000a;0;# in input mask and included this code to check if the employeeCode already exists in the Before Update.
But when i am trying to input a duplicate employee code. It's allowing it.
Any idea?
Private Sub Empl_Code_BeforeUpdate(Cancel As Integer)
If IsNull(DLookup("[EmplCode]", _
"FORM EMPLOYEE", _
"[EmplCode] = """ & Me.Empl_Code.Text & """")) = False Then
Cancel = True
MsgBox "Record already exists", vbOKOnly, "Warning"
Me![Empl Code].Undo
End If
End Sub
Your code looks legit.
Test your IsNull(DLookup... expression in Immediate window (View - Immediate window in VBA Editor) when you are trying to input a duplicate code.
Also you can shorten a bit your expression with DCount like that:
Private Sub Empl_Code_BeforeUpdate(Cancel As Integer)
If DCount("*", _
"FORM EMPLOYEE", _
"[EmplCode] = """ & Me.Empl_Code.Text & """") > 0 Then
Cancel = True
MsgBox "Record already exists", vbOKOnly, "Warning"
Me![Empl Code].Undo
else
' Just for test
MsgBox "Value: " & Me.Empl_Code.Text
End If
End Sub
Having a subform displaying the same table used in recordset makes the table read only (error 3027). Getting rid of the subform fixes the problem. Similar setup works in my other form interface.
I tried to delete the subform, which fix the accessibility problem. But that would defeat the original UX purpose.
Option Explicit
Public UnitRS As DAO.Recordset
Public ModelRS As DAO.Recordset
Public NameUnitRecords As String
Public NameModelRecords As String
Public Sub Form_Load()
'Initialization
NameUnitRecords = "tblBatteriesMainRecordsUnits"
NameModelRecords = "tblBatteriesRecordsModels"
End Sub
Private Sub SetUnitRecordsets()
'Set the path to the Battery Records table
Set UnitRS = CurrentDb.OpenRecordset(NameUnitRecords, dbOpenDynaset)
End Sub
Private Sub txtBatteryID_AfterUpdate()
'Set the recordset path for unit records
Call SetUnitRecordsets
'do a findfirst search for the Battery ID, using value from textbox txtBatteryID
UnitRS.FindFirst "[Battery ID]=" & txtBatteryID
'If no matching record, leave the other fields empty
If UnitRS.NoMatch Then
'If there is a matching record, then, grab the model number
Else
'as there is an existing record with model number, run a search on the model number and grab the model info
End If
'close recordset
UnitRS.Close
'check if the button can be enabled
End Sub
Private Sub cmbModelNumber_AfterUpdate()
'Set the recordset path for model records
'do a findfirst search for the Model Number, using value from combobox cmbModelNumber
'If no matching record, leave the other fields empty
If ModelRS.NoMatch Then
'If there is a matching record, then, grab the Model Info
Else
End If
'close recordset
'check if the button can be enabled
End Sub
Private Sub btnSaveAndCLear_Click()
Dim Response, strOldModelNumber
'Set the recordset path for unit records
Call SetUnitRecordsets
'Set the recordset path for model records
'close all related tables, queries and forms
DoCmd.Close acTable, NameUnitRecords, acSaveYes
DoCmd.Close acForm, "frmSubBatteriesMainRecordsUnits", acSaveYes
'If a new model record is required
ModelRS.FindFirst "[Model Number]='" & cmbModelNumber & "'"
If ModelRS.NoMatch Then
'msg box confirm model information
'If user chooses yes
If Response = vbYes Then
'create new model record
'this block could be done with "With...End" format for less error vulerability?
'nah, unless it is repetitively called, it's too much work to inplement just the .addnew part
'requery the two combobox to reflect newest changes
'User chooses no
Else
'popup a message telling the user the battery and model record is not logged, but don't clear the field.
Exit Sub
End If
End If
'need to find the record first, otherwise it will edit the first one
UnitRS.FindFirst "[Battery ID]=" & txtBatteryID
'store the old model number value
strOldModelNumber = UnitRS("Model Number")
'New record
If UnitRS.NoMatch Then
'create a new battery record
UnitRS.AddNew
UnitRS("Battery ID") = Me.txtBatteryID
'If this is an edit on existing record
Else
'if the new value is the same as the old one
If strOldModelNumber = cmbModelNumber Then
'msgbox the same value, no change is done to the database
MsgBox "the data is the same as the old record, no change is made to the record", vbInformation, "Same data"
Exit Sub
Else
'msg box confirm edit
Response = MsgBox("Please confirm edit on existing record: " & Chr(13) & Chr(10) & "BatteryID: " & txtBatteryID & Chr(13) & Chr(10) & "Model Number: " & cmbModelNumber, vbYesNo, "New Model Record Dectected")
'If user chooses yes
If Response = vbYes Then
'goto edit mode
UnitRS.Edit
'if user chooses no
Else
'msgbox notify the user nothing is changed in the database
MsgBox "Battery and Model record not logged, you may retry logging", vbInformation, "Record not logged"
Exit Sub
End If
End If
End If
'both changes the model number and comment field anyway
UnitRS("Model Number") = Me.cmbModelNumber
UnitRS("Comment") = Me.txtComment
'commit update
UnitRS.Update
UnitRS.Close
'clear all flieds
'reset all locks
'requery the sub form to reflect the newest changes
Me.subFrmBatteryRecords.Requery
End Sub
What I would like to achieve is to have a display in the form interface to show the content of the actual record table, so that the user knows what is in the table.
There is nothing wrong with my code. There is a property option in the form object, it's called "record lock", somehow mine had "All Records" selected. By making it "No locks", the accessibility problem is gone.
I Want to be able to let the end user of the database know when they are on a new record, but I do not want to display the actual id's. I just want the text box to display "New" when it is a new record.
I have two buttons one the selects the previous record and the other that selects the next record. The next record button has the code that I am trying to get to work.
Private Sub Command25_Click()
On Error GoTo Command25_Click_Err
On Error Resume Next
DoCmd.GoToRecord , "", acNext
' I wrote this if statment to capture the (New)
If frmQuote_QuoteID.Value = " " Then
frmQuote_QuoteNumber.Value = "NEW"
End If
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If
Command25_Click_Exit:
Exit Sub
Command25_Click_Err:
MsgBox Error$
Resume Command25_Click_Exit
End Sub
I have also tried if frmQuote_QuoteID.value = "(New)" Then
I am trying to get this to the point where the form can display New based on an empty primary key field, but if it is not a new record then I don't want anything displayed
You need to use
if me.NewRecord then