I can get the following code to run manually on a button but it will not do anything when put into the on load event of the form. I'm sure I'm missing something obvious but can't see what it is!
"newenquirysiteslistq" is a query of address's based on the name of the customer within the form.
Private Sub Form_Load()
If Nz(dcount("*", "newenquirysiteslistq"), 0) < 2 Then
Me.previous_sites.Visible = False
Else
Me.previous_sites.Visible = True
End If
End Sub
Related
I have built a form in MS Access that creates datasets for a database.
I have then implemented some code to check if all required fields have been filled before permitting the form to be closed and the dataset to be saved to the database.
All is working well, but I can't seem to suppress the standard error message created by Access telling me that I cannot save the dataset at the moment (if a required field is not filled and Cancel is set to true) and aksing me if I wanted to exit the form without saving.
The structure of my code is as follows:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If requiredFieldsNotFilled Then
Cancel = True
Else
Cancel = False
End If
End Sub
When Cancel is set to true (i.e. the required fields haven't all been filled) a standard error message is thrown by Access (Error Code 2169) which I am trying to suppress as such:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub
However, the generated error message provides a yes/no choice. 'Yes' being close the dataset without saving, 'No' being not close the dataset.
If I use the error handler as described above the default choice is taken which in this case is 'Yes' and the dataset is closed without being saved!
This is not what I want! I want 'No' to be chosen so that the user stays on the form and can correct his inputs... Any ideas on how to do that?
Any help is much appreciated! Thanks in advance!
I don't think such functionality is available, but you can manually stop the next close action by using the Form_Unload event:
Dim DontClose As Boolean
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then
Response = acDataErrContinue
DontClose = True
Else
Response = acDataErrDisplay
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cancel = DontClose
DontClose = False
End Sub
I would like to have a button to open a form and then run VBA code on that form. So either using Form_Load or and intermediate module. Does anyone know how to do this?
Thanks
Use OpenArgs.
First form:
DoCmd.OpenForm "SecondForm", OpenArgs:=Me.Name
Second form:
Private Sub Form_Load()
If Me.OpenArgs = "FirstForm" Then
' Stuff
End If
End Sub
Declare a module level variable in the second form:
Dim Prev As Form
In the On Load-event of the second form (this sets a reference to the first form):
Set Prev = Screen.ActiveForm
And in the On Close-event:
Set Prev = nothing
Now you can check the name of the previous form with:
If Prev.Name = "..." Then
... your actions
End If
Furthermore you can check any property or field from the first/previous form this way. Prev is acting like Me now.
Lets say the new form be opened from a number of forms in your application;
Can more than one of the calling forms be open at any one time?
If not, use this:
Private Sub Form_Load()
If isLoaded("Form1") then
Form1_InstructionSet
ElseIf isLoaded("Form2") then
Form2_InstructionSet
...
End If
End Sub
Private Sub Form1_InstructionSet
...
End Sub
etc.
If more than one of the calling forms can be open simultaneously, you should parameterise the new form, as per #Andre's answer above.
i have my regular form with a continous form attached to it. Everything works fine untill the subform has no data.
Here is the subform with data
image1
IF there are no data in the subform, the form becomes like this.
image 2
I am using the following code in my on current event on my main form:
Function RefreshCOunt()
If Me.frmVerifyEquipmentSub.Form.Recordset.RecordCount = 0 Then
Me.frmVerifyEquipmentSub.Visible = False
Me.txtTotal = 0
Me.Refresh
Else
Me.frmVerifyEquipmentSub.Visible = True
Me.frmVerifyEquipmentSub.Form.Requery
Forms![test3].txtTotal = Me.frmVerifyEquipmentSub.Form.txtSum
Me.Refresh
Forms![test3].Refresh
End If
End Function
Private Sub Form_Current()
Call RefreshCOunt
End Sub
Private Sub Form_GotFocus()
Call RefreshCOunt
End Sub
Here is the code on my subform, THis function is run when my user clicks the save button.
Function RefreshCOunt()
If Me.txtCount = 0 Then
Forms![test3].SetFocus
Else
Me.Requery
Forms![test3].txtTotal = Me.txtSum
Me.Refresh
Forms![test3].Refresh
End If
End Function
I have a text behind the subform which says "NO Available Data to be shown"
2 problems in this situation are:
Empty form view as shown on image 2 (resolved as i had linked the record source of the main form to the table also used in subform)
Hide the subform once the user has verified all the equipment.
-currently my code only works the first time the form is loaded.
-the subform does not hide once the user has verifed all the equipment
I know how to do this in report with the HAsData function but i am not sure how i can do so in Forms.
It sounds like your query for the main form has an inner join to the table (or a query with this table) for the subform.
Remove this join or change it to an outer join.
I'm sure this is very simple, but I can't find it. In the close event of an Access Form, how can I cancel closing the form? I have a test that counts the records in a table. If that table has records, I want to ask the user if they want to close or go back and work with them. So how do I cancel the close event?
You can use the Unload event:
GlobalVar ButtonClicked
Private Sub Form_Open(Cancel As Integer)
ButtonClicked = False
End Sub
Private ClickMe_Click(Cancel As Integer)
ButtonClicked = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not ButtonClicked Then
Cancel = True
End if
End Sub
Order of events for database objects
Study and try this code, it worked for me. Replace necessary variable names with your chosen names. Paste the code in the form_unload Event of your form.
WARNING!!!: After you perform this operation you will find it difficult to access your form in design and layout view
Private Sub Form_Unload(Cancel As Integer)
userresponse = MsgBox("Are you sure you want close? All your work wouldn't be saved", vbYesNo, "Database Information")
Select Case userresponse
Case 6
Cancel = False
'this line opens another form in my own case
DoCmd.OpenForm "EngMenu"
Case 7
Cancel = True
'this line keeps my own form open in my own case
DoCmd.OpenForm "UpdateForm"
Case Else:
MsgBox "You are not allowed to perform this operation", vbInformation, "Database Information"
End Select
End Subenter code here
Use the "Form_BeforeUpdate(cancel as integer)" event and set cancel to True.
Notice that you simply will not be able to close at all unless you add some logic to actually allow updating the database.
im trying to check if a number is a text or number in a banking system. If the value is incorrect it should erase and send an error message, but my code isnt working at all... can someone help me? here is the code:
Private Sub TextBox1_Change()
name = TextBox1
If Application.IsText(name) = True Then
IsText = True
Else
MsgBox "Insert only words"
Selection.name.Delete
End If
End Sub
I think you should be using name = TextBox1.Text instead. It's probably giving you an error on that line.
Also, you may want to wait until the user is finished typing the response, I believe the changed event will run each time a character is pressed. The AfterUpdate will run after you tab or click away from the textbox.
And really, instead of deleting the response (which I don't think will work), you should simply set the textbox blank back to an empty string.
Private Sub TextBox1_AfterUpdate()
If Not MyIsANumber(TextBox1.Text) Then
MsgBox ("It's Text")
Else
TextBox1.Text = ""
End If
End Sub
Function MyIsANumber(str As String) As Boolean
For i = 1 To Len(str)
If IsNumeric(Mid(str, i, 1)) Then
MyIsANumber = True
Exit Function
End If
Next
MyIsANumber = False
End Function
I'm sure the code could be structured better, but there's something along the lines of what you need. In this case, it just wipes the textbox if there is a number entered. Obviously, things will need to be the opposite for your textbox that you want to contain a number.
Have you considered the vartype() function? https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/vartype-function