I'm making a form with Microsoft Access and I'm trying to make an AfterUpdate event procedure for a field. I need to know if the value of that field is the default value (or if it's empy). I read that the default value of a field in VBA is Null so I made this:
Private Sub EB1_10_Val1_AfterUpdate()
If Me.EB1_10_Val1.Value = Null Then
MsgBox "hello"
End If
End Sub
This didn't work so I tried this for when the user updates the value in the field and then erases it (empties the field)
Private Sub EB1_10_Val1_AfterUpdate()
If Me.EB1_10_Val1.Value = Empty Then
MsgBox "hello"
End If
End Sub
The messages never pop up. On the other hand I tried changing the default value of the field to 0 but Its not working. The 0 doesn't appear in the field as default when in Form View.
You can check if the field is empty with this expression:
If IsNull(Me.EB1_10_Val1.Value) Then
In addition to solution provided by mielk, you can use Nz or Iif function to replace Null with default value.
Usage:
myVal = Nz(Me.SomTextBox, 0)
Related
I have a field called country with multi valued combo box. There are 10 values in the combo box. I need another field to be visible when a value from country field is selected otherwise invisible. I tried to use below VBA script but it gives an error as below
Error:
Runtime error 13
types incompatible
I am using the code below for which I get the above error:
Private Sub country_Click()
If country.Value = "11. OTHER" Then
Me.country_txt.Visible = True
Else
Me.country_txt.Visible = False
End If
End Sub
I am beginner to use MS Access. Can anyone please help.
I am able to solve the issue by slightly changing the script. I used the below script and it works fine.
Private Sub country_Click()
If Me.country.Selected(10) Then
Me.country_txt.Visible = True
Else Me.country_txt.Visible = False
End If
End Sub
So here the issue got resolved when the value of the combo box was called as integer. In the list "11. OTHERS" is the 11th value. So Me.country.Selected(10) did the trick.
I'm creating a Form in Microsoft Word. I have several 'Text form fields' some of which I want to restrict so users can only enter numbers. I don't understand why Microsoft gives you the option to change the 'Type' to 'Number' when that still allows any value to be input. Since that seems to be the case I have turned to VBA.
I'm trying to run a macro when the user exits one of these fields, to make sure the input is valid. I would rather not create a new macro for each field I want to restrict to numeric.
I think my problem is that I don't know how to get the result of the current field. I could create a different macro for each field, and get the result by specifying its name explicitly, but it seems like a smarter way would exist.
Here's what I have so far.
Sub validateNumericFields()
'Check to see if the value in the current field is numeric
'If it is not, send a message to the user, and set value to default value
If IsNumeric(Selection.Fields(1).Result) = False Then
MsgBox "You must enter a valid number.", vbExclamation
Selection.Fields(1).Result = Selection.Fields(1).TextInput.Default
End If
End Sub
There are various ways to get the "name" of a form field, since this is also a bookmark. The FormField object does have a Name property, but I can't get to it from the Selection object available when OnExit runs. But I can get the bookmark name, so:
Sub validateNumericFields()
'Check to see if the value in the current field is numeric
'If it is not, send a message to the user, and set value to default value
Dim ff As word.FormField
Set ff = ActiveDocument.FormFields(Selection.Bookmarks(1).Name)
If IsNumeric(ff.Result) = False Then
MsgBox "You must enter a valid number.", vbExclamation
ff.Result = ff.TextInput.Default
End If
End Sub
I have a combo box (Status) which includes the following:
Shortage
Allocated
Actioned
Acknowledged
Complete
I also have 5 other date fields which are as follows:
Shortage_date
Allocated_date
Actioned_date
Acknowledged_date
Complete_date
However I want this status to be populated automatically based on what data has been entered in my previous fields.
For example, once shortage_date has been populated with a valid date (00/00/0000) I want the "status" to change to "shortage".
Once allocated_date has been populated with a valid date (00/00/0000) I want the "status" to change to "allocated".
I saw this bit of code online but I'm totally confused:
Private Sub Textbox1_AfterUpdate()
If Textbox1.Value = "1" Then
Textbox2.Value = "10"
End If
End Sub
I believe mine should look something like this but I dont know what I need to make sure it validates the date.
Private Sub shortage_date_AfterUpdate()
If shortage_date.Value = "(I want to valididate the date here)" Then
Status.Value = "Status"
End If
End Sub
Hope I make sense!
Firstly, I would set up an Input Mask on the field itself. You can do that by putting the form into design view, then select the field, then go the Property Sheet which isAlt+Enter if it isn't open, then select the Data tab and set up a Input Mask. That will handle your validation part so you don't have to in code.
Then you should be able to just use the code:
Private Sub shortage_date_AfterUpdate()
If Nz(shortage_date.Value, "") <> "" Then
Status.Value = "Status"
End If
End Sub
The If statement is just to make sure that it doesn't reset the value back to the original every time the date is changed. Also here is link where you can read about Input Masks: https://msdn.microsoft.com/en-us/library/office/ff821336.aspx
Update: Changed to Input Mask instead of Validation Rule
In my user form I have a textbox and only numeric values are allowed to be input into this textbox, once a numeric value is input and the button next to the texbox is clicked it is supposed to do a calculation on the numeric value and add it to my listbox. When I click the button to add the numeric value I get an error object required. Below shows how I am trying to execute this process
userInput= Textbox1.Value
List.AddItem executeFormula(userInput)
Function executeFormula(inputs As Integer)
inputs = inputs * 5
End Function
I have narrowed down the issue, the function is working perfectly but it is when I am trying to add the function onto the end of the List.AddItem
Have you defined Option Explicit at the top of your code? Is the List object correctly named based on what you have on your form? I suspect this is the old VBA equivalent of a null reference exception and VBA isn't able to get a working reference to that listbox.
You could try using:
Set List = UserForm1.List ' or whatever the name of your form is.
Two small problems:
your function should return its result thru the function name
you should explicitly add a String
starting with a blank "forms-style" listbox:
Sub trewr()
Dim lb As ListBox, ii As Integer
Set lb = ActiveSheet.ListBoxes(1)
ii = 56
lb.AddItem (CStr(executeFormula(ii)))
End Sub
Function executeFormula(inputs As Integer) As Integer
executeFormula = 5 * inputs
End Function
I would like to be able to change the Textbox.DefaultValue during the 'On Load' event of a form such that each time the form is loaded the user is prompted with an InputBox to change a specific TextBox.Default value which in my case the TextBox control on the table is called Stream. I have tried the following code but each time it gives me a
'RunTime Error 3422 Cannot modify table structure. Another user has
the table open'.
Private Sub Form_Load()
CurrentDb.TableDefs("Class 1 Students (C1)").Fields("Stream").DefaultValue = InputBox("Enter Stream Letter:")
End Sub
I am using Microsoft Access
As Doug Glancy said in a comment, don't change the field's default value in table design. Instead change the text box's default value.
This is a critical point in a multi-user database application --- you wouldn't want one user stomping on another's default value choice. But, even if this will always be a single-user application, changing the table design means you can't have the table open in the record source of your form.
Changing the text box default value is easy. I added an unbound text box, txtDefaultStream, to my form's header. And, in its after update event, I change Me.txtStream.DefaultValue. The code is below.
Here is a screenshot of that form in action. I had A as the default when entering the first 2 rows. Then entered B in the default stream text box. Notice the new record has B in its Stream text box.
Private Sub txtDefaultStream_AfterUpdate()
Dim strDefault As String
If Len(Trim(Me.txtDefaultStream & vbNullString)) > 0 Then
strDefault = """" & Me.txtDefaultStream.value & """"
Me.txtStream.DefaultValue = strDefault
Else
Me.txtStream.DefaultValue = vbNullString
End If
End Sub
As Doug Glancy said, use the textbox, so you should try
Private Sub Form_Load()
Me.AText.DefaultValue = "='S'"
End Sub