ISSUE
I have two forms:
frmForms
frmDockRental
I have two controls associated with this issue:
lstOwners on frmForms (unbound)
cboOwner on frmDockRental (unbound)
The second form (frmDockRental) is opened using different listboxes located on frmForms (See Image). I have one such listbox that's giving me grief. It is a filtered list of contacts that when double-clicked it should be opening frmDockRental to a NEW record and set cboOwner, which is unbound, to a specific item in the list. The same item listed in lstOwners from frmForms.
CODE
After a lot of fiddling, I came up with this - except nothing happens at all.
Private Sub lstOwners_DblClick(Cancel As Integer)
On Error GoTo lstOwners_DblClick_Err
On Error Resume Next
If (Form.Dirty) Then
DoCmd.RunCommand acCmdSaveRecord
End If
If (MacroError.Number <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
Exit Sub
End If
On Error GoTo 0
Exit Sub
DoCmd.OpenForm "frmDockRental", acNormal, "", "", acFormAdd, acDialog, Me.lstOwners
DoCmd.Close acForm, Me.Name
lstOwners_DblClick_Exit:
Exit Sub
lstOwners_DblClick_Err:
MsgBox Error$
Resume lstOwners_DblClick_Exit
End Sub
And on frmDockRental, this:
Private Sub Form_Load()
On Error GoTo Form_Load_Err
DoCmd.MoveSize , , 5.3 * 1440, 5.2 * 1440
' If (IsNull(OpenArgs)) Then
' Exit Sub
' End If
If Me.OpenArgs <> vbNullString Then
Me.cboOwner = Me.OpenArgs
End If
If (Not IsNull(OpenArgs)) Then
DoCmd.GoToRecord , "", acNewRec
End If
Form_Load_Exit:
Exit Sub
Form_Load_Err:
MsgBox Error$
Resume Form_Load_Exit
End Sub
I figured OpenArgs would be the best method to accomplish this, but it just doesn't work. Nothing at all happens. No errors, just nothing.
EDIT:
Here's an image of the step debugging.
Related
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
On the Form_beforeupdate() event of my form "Alias" I have this...
If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then
MsgBox "You must enter a contact!", vbokayonly, "Contact"
Cancel = True
End If
Anytime this cancels a runCommand code such as...
DoCmd.RunCommand acCmdSaveRecord
a dialog appears telling me that it was canceled. Is there a way to suppress those dialogs?
You can add an error handler to trap and ignore error 2501, which is triggered when the form's Before Update event cancels DoCmd.RunCommand acCmdSaveRecord
The following example is from my form which has a command button to save the current record. It suppresses that "RunCommand action was canceled" message as I think you wish.
Since your form's code apparently calls DoCmd.RunCommand acCmdSaveRecord from multiple locations, replace each of those calls with SaveRecord as I did in cmdSave_Click().
Option Compare Database
Option Explicit ' <-- NEVER troubleshoot VBA code without this!!!
Private Sub cmdSave_Click()
'DoCmd.RunCommand acCmdSaveRecord
SaveRecord
End Sub
Private Sub SaveRecord()
Dim strMsg As String
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 2501 ' The RunCommand action was canceled.
' do nothing --> just ignore the error
Case Else
' notify user about any other error
strMsg = "Error " & Err.Number & " (" & Err.Description _
& ") in procedure SaveRecord"
MsgBox strMsg
End Select
Resume ExitHere
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtBlock_start.Value) Then
MsgBox "You must enter a start time!", vbOKOnly, "Start Time"
Cancel = True
End If
End Sub
I ended up doing this on a 'Save and Open the Receipts form' button.
Private Sub btnSaveOpenReceipts_Click()
'''''SaveRecord'''''
If (Me.Dirty) Then
SaveRecord
End If
'''''OpenReciepts'''''
If (Me.Dirty) Then 'unsure it saved
Exit Sub
Else
'blah blah blah
End If
End Sub
I have a form "Form1" with a command button that when clicked, hides itself, and then opens another form "Form2" as follows:
Private Sub Command1_Click()
Me.Visible = False
DoCmd.OpenForm "Form2"
End Sub
The Form_Open subroutine of Form2 contains a check for errors. If an error is found, I want Form2 to display an error, close Form1, and then close itself:
Private Sub Form_Open(Cancel As Integer)
AnError = 'check for an error
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
DoCmd.Close
End If
End Sub
I've tried numerous variations on this scheme for opening and closing the forms, and all fail in different ways. This particular one seems to do everything except actually close Form2 after the error message is displayed.
Once again I'm baffled about the way Access 2010 VBA handles the opening and closing of forms; the reference information at MSDN is atrocious. Thanks for any help.
You are close. - this code will also close Form2:
Private Sub Form_Open(Cancel As Integer)
'AnError = 'check for an error
If Not True Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
DoCmd.Close acForm, Me.Name 'Me.Name is used for illustration purposes, you can also use "Form2"
End If
End Sub
I've always seen this done via the UnLoad keyword
Try this,
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
Unload Form1
'Unload Me
'OR since you can cancel this form loading
Cancel = True
End If
Try changing to the Me object:
Private Sub Form_Open(Cancel As Integer)
AnError = 'check for an error
If Not AnError Then
'things are OK
Else
Response = MsgBox("There was an error in Form2.", 0, "Form2 error message")
DoCmd.Close acForm, "Form1"
Me.Close
' or possibly Unload Me
End If
End Sub
I have a command button tied to a query called "GenderCount". The results appear in a subtable when I click the button. I need the results to appear in a text box on my form (Text26). Here is my code--thanks in advance for any suggestions:
Private Sub Command21_Click()
On Error GoTo Err_Command21_Click
Dim stDocName As String
stDocName = "GenderCount"
DoCmd.OpenQuery stDocName, acNormal, acEdit
Exit_Command21_Click:
Exit Sub
Err_Command21_Click:
MsgBox Err.Description
Resume Exit_Command21_Click
End Sub
This what you need
Private Sub Command21_Click()
On Error GoTo Err_Command21_Click
me.txtField1 = dlookup("CountOfGender","GenderCount","Gender = M")
me.txtField1 = dlookup("CountOfGender","GenderCount","Gender = F")
Exit_Command21_Click:
Exit Sub
Err_Command21_Click:
MsgBox Err.Description
Resume Exit_Command21_Click
End Sub
Warning: I am very new to Access coding.
I am used to .NET and PHP
I just converted an Access 95 program to Access 2007 and I got it all working now I am trying to make some updates.
I have a form that updates a table in the database but I can't find an attached query or anywhere where it actually does a SQL statement. Please help. And thank you.
This is all the code on the one form.
Option Compare Database
`enter code here`Option Explicit
Private Sub Check158_Click()
' Try and change sql code
Me.Combo35.RowSource = "SelectAllOrders"
End Sub
Private Sub Combo35_AfterUpdate()
Me.RecordsetClone.FindFirst "[OrderID] = " & Me![Combo35]
Me.Bookmark = Me.RecordsetClone.Bookmark
' Find the record that matches the control.
End Sub
Private Sub Command122_Click()
On Error GoTo Err_Command122_Click
DoCmd.Close
Exit_Command122_Click:
Exit Sub
Err_Command122_Click:
MsgBox Err.Description
Resume Exit_Command122_Click
End Sub
Private Sub Customer_AfterUpdate()
Forms![Orders]![CustomerID] = [Forms]![Orders]![Customer].Column(0)
Forms![Orders]![Phone] = [Forms]![Orders]![Customer].Column(2)
End Sub
Private Sub Frame40_AfterUpdate()
'Forms![Orders]![Orders Subform].Form![Quantity]
If Forms![Orders]![Frame40] = 1 Then
' Me![Orders By Customer Subform].[Requery
Forms![Orders]![Child81].Form![ProductID].RowSource = "StdPrice"
Else
Forms![Orders]![Child81].Form![ProductID].RowSource = "PvtPrice"
End If
End Sub
Private Sub Command131_Click()
On Error GoTo Err_Command131_Click
Me![Invoice Total] = Forms![Orders]![Child81].Form![Order Subtotal]
DoCmd.GoToRecord , , acNewRec
Exit_Command131_Click:
Exit Sub
Err_Command131_Click:
MsgBox Err.Description
Resume Exit_Command131_Click
End Sub
Private Sub Command132_Click()
On Error GoTo Err_Command132_Click
'Cancel Click for Orders
If Me.Dirty Then DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Me![Invoice Total] = Forms![Orders]![Child81].Form![Order Subtotal]
DoCmd.Close
Exit_Command132_Click:
Exit Sub
Err_Command132_Click:
MsgBox Err.Description
Resume Exit_Command132_Click
End Sub
Private Sub Command133_Click()
On Error GoTo Err_Command133_Click
'Save and Exit Click
Me![Invoice Total] = Forms![Orders]![Child81].Form![Order Subtotal]
DoCmd.Close
Exit_Command133_Click:
Exit Sub
Err_Command133_Click:
MsgBox Err.Description
Resume Exit_Command133_Click
End Sub
Private Sub Command134_Click()
On Error GoTo Err_Command134_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
Exit_Command134_Click:
Exit Sub
Err_Command134_Click:
MsgBox Err.Description
Resume Exit_Command134_Click
End Sub
Private Sub Command136_Click()
On Error GoTo Err_Command136_Click
DoCmd.GoToRecord , , acNext
Exit_Command136_Click:
Exit Sub
Err_Command136_Click:
MsgBox Err.Description
Resume Exit_Command136_Click
End Sub
Private Sub Command138_Click()
On Error GoTo Err_Command138_Click
Me![Invoice Total] = Forms![Orders]![Child81].Form![Order Subtotal]
DoCmd.GoToRecord , , acNext
Exit_Command138_Click:
Exit Sub
Err_Command138_Click:
MsgBox Err.Description
Resume Exit_Command138_Click
End Sub
Private Sub Command148_Click()
On Error GoTo Err_Command148_Click
Dim stDocName As String
stDocName = "Container Card"
DoCmd.OpenReport stDocName, acPreview, , "[OrderID] = Forms![Orders]![OrderID]"
Exit_Command148_Click:
Exit Sub
Err_Command148_Click:
MsgBox Err.Description
Resume Exit_Command148_Click
End Sub
Private Sub Form_Close()
' Me![Invoice Total] = Forms![Orders]![Child81].Form![Order Subtotal]
End Sub
And a SUB form
Option Compare Database
Option Explicit
Private Sub Form_AfterDelConfirm(Status As Integer)
Forms![Orders]![Child81].Requery
End Sub
Private Sub Form_Open(Cancel As Integer)
'If Forms![Orders]![Frame40] = 1 Then
' Me![ProductID].RowSource = "StdPrice"
' Else
' Me![ProductID].RowSource = "PvtPrice"'
'End If
End Sub
Private Sub ProductID_NotInList(NewData As String, Response As Integer)
MsgBox "Double-click this field to add an entry to the list."
Response = acDataErrContinue
End Sub
Private Sub ProductID_AfterUpdate()
Dim pos As Variant
Me![UnitPrice] = Me![ProductID].Column(2)
Me![ProductName] = Me![ProductID].Column(1)
Me![GLAcct] = Me![ProductID].Column(3)
Me.Dirty = False
End Sub
Private Sub ProductID_DblClick(Cancel As Integer)
On Error GoTo Err_ProductID_DblClick
Dim lngProductID As Long
If IsNull(Me![ProductID]) Then
Me![ProductID].Text = ""
Else
lngProductID = Me![ProductID]
Me![ProductID] = Null
End If
DoCmd.OpenForm "Products", , , , , acDialog, "GotoNew"
Me!ProductID.Requery
If lngProductID <> 0 Then Me![ProductID] = lngProductID
Exit_ProductID_DblClick:
Exit Sub
Err_ProductID_DblClick:
MsgBox Err.Description
Resume Exit_ProductID_DblClick
End Sub
Private Sub Command20_Click()
On Error GoTo Err_Command20_Click
DoCmd.GoToRecord , , acNewRec
Exit_Command20_Click:
Exit Sub
Err_Command20_Click:
MsgBox Err.Description
Resume Exit_Command20_Click
End Sub
Private Sub Command22_Click()
On Error GoTo Err_Command22_Click
DoCmd.GoToRecord , , acNext
Exit_Command22_Click:
Exit Sub
Err_Command22_Click:
MsgBox Err.Description
Resume Exit_Command22_Click
End Sub
Private Sub Command23_Click()
On Error GoTo Err_Command23_Click
DoCmd.GoToRecord , , acFirst
Exit_Command23_Click:
Exit Sub
Err_Command23_Click:
MsgBox Err.Description
Resume Exit_Command23_Click
End Sub
Private Sub Command24_Click()
On Error GoTo Err_Command24_Click
DoCmd.GoToRecord , , acPrevious
Exit_Command24_Click:
Exit Sub
Err_Command24_Click:
MsgBox Err.Description
Resume Exit_Command24_Click
End Sub
Private Sub Command25_Click()
On Error GoTo Err_Command25_Click
DoCmd.GoToRecord , , acLast
Exit_Command25_Click:
Exit Sub
Err_Command25_Click:
MsgBox Err.Description
Resume Exit_Command25_Click
End Sub
Private Sub Command26_Click()
On Error GoTo Err_Command26_Click
DoCmd.GoToRecord , , acNext
Exit_Command26_Click:
Exit Sub
Err_Command26_Click:
MsgBox Err.Description
Resume Exit_Command26_Click
End Sub
Private Sub Quantity_AfterUpdate()
Dim pos As Variant
'Me.Dirty = False
End Sub
Private Sub UnitPrice_AfterUpdate()
Dim pos As Variant
'Me.Dirty = False
End Sub
Private Sub Command33_Click()
On Error GoTo Err_Command33_Click
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Exit_Command33_Click:
Exit Sub
Err_Command33_Click:
MsgBox Err.Description
Resume Exit_Command33_Click
End Sub
The form in your first example is likely a databound form - it should have a datasource set, which will be the query or table used to populate the fields on the form. If you edit values on the form, and close the form, it will automatically commit changes to the tables underlying that query.
The second example has some navigation buttons...navigating away from the current record should automatically cause a commit as well. Again, look at the datasource property of the form to work out what it's updating.