I'm trying to use a Select Case to evaluate a TempVar string value, but the case is not giving me the correct result.
My VBA Code so far:
Private Sub Form_Load()
Select Case TempVars!CurrentStatus
Case Entered
'code to disable some controls
Case In Progress
'code to disable some controls
Case Ready to Approve
'code to disable some controls
Case Approved
'code to disable some controls
Case Else
Call MsgBox("Case statement didn't catch", vbInformation, "Tempvar test for case statements")
End Select
End Sub
The TempVar is set as follows in another form:
Dim StrUserInput1 As String
TempVars("CurrentStatus").Value = DLookup("Status", "Control", "Status = '" & StrUserInput1 & "'")
'This is set by comparing the user input vs allowed status (for reports which is yet to be implemented)
I've tried setting the Case as Case "Entered" but it always select the first case, not the correct one. If its formatted as show above, the result is Case Else
Can anyone offer some insight on why or how I'm not getting the correct result?
I'm surprised the code even runs and doesn't throw an error. You need to have the string literals in quotes, like this:
Select Case TempVars!CurrentStatus
Case "Entered"
code to disable some controls
Case "In Progress"
code to disable some controls
Case "Ready to Approve"
'code to disable some controls
Case "Approved"
'code to disable some controls
Case Else
Call MsgBox("Case statement didn't catch", vbInformation, "Tempvar test for case statements")
End Select
If that doesn't work, put a breakpoint on Select Case TempVars!CurrentStatus to see what the value of TempVars!CurrentStatus is when it enters the case expression.
Related
I have a textBox formatted as "Short Date". When I put invalid data in field, for example random "dfsdf", and try to change focus, form throws validation Error 2113.
My goal is to give an opportunity to user to close form by click on "Cancel" button without any problem, because no matter what he entered in Date textbox while form is canceled.
I can handle this error and disable message with Form_Error event, but focus stays set to date textBox anyway, when i try to click Cancel button, and button is never clicked.
I use a setup, probably similar to yours:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const InvalidDate As Long = 2113
Select Case DataErr
Case InvalidDate
MsgBox "Invalid date!"
Response = acDataErrContinue
' Cancel invalid entry.
Me.Undo
' DoCmd.Close
Case Else
Response = acDataErrDisplay
End Select
End Sub
Problem is, that this will fire before your button gets focus. This means, that the form doesn't even know what you clicked, only that it was somewhere outside the date textbox.
Thus, the DoCmd.Close would close the form for whatever was clicked.
You may turn it into an advantage, that the user is able to watch the removal of the faulty input ...
You should use the after update event to check if the user entered a valid date,
if not then clear or undo. Without knowing the controls of your form i can't tell if you allow users to change table values or if you are using transactions.
pseudo code:
Private Sub Text1_AfterUpdate()
If Check_If_Date Then
Execute_Code
Else
End If
End Sub
Private Function Check_If_Date() as Boolean
If IsDate(Text1) then
Check_If_Date = True
Else
Check_If_Date = False
Select Case msgbox("Not a valid date", vbOKCancel)
case vbOK
reset_value
case vbCancel
Close_Form
case else
End Select
End If
End Sub
Private Sub Reset_Value()
'need to clear value or undo
End Sub
Public Sub Execute_Code()
'Code to save values or allow Update to complete
End Sub
Private Sub Close_Form()
'Code For Closing Form and logging errors
End Sub
I split up repeatable actions for reuse in case the same thing can be repeated in another text box for example a second date or to get the same behavior for the other controls.
Is there any way I can let a user know that the field in the current record is identical to the field in another record? Because 99% of the time they will be different, but 1% of the time there will have to be two records with the same field, so I want a way to alert the user and just make sure they are aware of that in case they did not mean to do it.
Write a Function and call it as needed. In your case in the AfterUpdate of the control as #Andre mentioned above.
Public Function IsDuplicate(ByVal Value As String) As Boolean
IsDuplicate = (DCount("*", "TableName", "FieldName='" & Value & "'") > 1)
End Function
You can call it like this:
Private Sub Text0_AfterUpdate()
If IsDuplicate(Me.Text0.Value) Then
MsgBox "Value exists..."
End If
End Sub
MS ACCESS 2007
VBA CODE BUILDER
I am able to show a MsgBox in VBA code when a Tab is selected with
Private Sub TabCtl34_Change()
If Me.TabCtl34.Value = 1 Then 'First Page
MsgBox "Hi"
End If
End Sub
But I also want to check if the second tab is selected and a field(Name) on the form IS NULL (not on tab), msgbox or cancel event to require Name to be entered before they see the second tab.
When I add in:
Private Sub TabCtl34_Change()
If Me.TabCtl34.Value = 1 AND [FORMS]![FORMNAME]![NAME] IS NULL Then
MsgBox "Hi"
End If
End Sub
It just goes directly to the second tab even if the name is null. How do i write the vba code so it can do what I want?
IS NULL is for use in query expressions, from VBA use the IsNull() VBA function.
If Me.TabCtl34.Value = 1 AND IsNull([FORMS]![FORMNAME]![NAME]) Then
As you are running this from the form's module you can use Me to refer to the current form-instance:
If Me.TabCtl34.Value = 1 AND IsNull(Me.[NAME]) Then
You should also consider that the value may be an empty string, rather than NULL. You can combine both checks using:
If Me.TabCtl34.Value = 1 And Len(Me.[NAME].Value & "") = 0 Then
Concatenating the empty string "" coerces a NULL value to a string.
(It is preferable to explicitly identify the property Value rather than assume it as the default property.)
Having a bit of trouble. I have a simple situation: 2 Radio buttons, Accept or Reject. Instead of doing IF statements I feel an obligation to do Select statements.
As I walk through my code, it gets to the Case rdbApprove.Value and complains that the value is null. I figure it isn't true, thus it should pass to the next Case. Nope - it hangs on the fact that it is null.
In an effort to try and get rid of the null value I tried the NZ(rdbApprove.Value, False) to try and tell it that it was false but that still isn't working.
Select Case True
Case rdbApprove.Value
Case rdbReject.Value
Case Else
MsgBox "ERROR!"
End Select
The usual way this is done is to include your radio buttons in an option group. Then your code can reference the option group .Value.
My form includes an option group named Frame15 which includes an "Approve" radio button whose Option Value is 1, and a "Reject" button whose Option Value is 2.
I also added a command button to inspect the option group .Value. Here is its click event procedure.
Private Sub cmdOptionValue_Click()
Dim strPrompt As String
Select Case Me.Frame15.value
Case 1
strPrompt = "approved"
Case 2
strPrompt = "rejected"
Case Else
If IsNull(Me.Frame15.value) Then
strPrompt = "Null"
Else
strPrompt = "this should not happen"
End If
End Select
MsgBox strPrompt
End Sub
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