Enable/Disable Fields with certain criteria MS Access - vba

I have a form in MS Access that I'm trying to create for insurance claims. I have all the fields that I need to be filled in but what I'd like to be able to do is enable or disable those fields depending on certain actions of the users. So the flow of the form is like this: I have a Frame at the top with two radio buttons, one for a single-claim incident and one for a multi-claim incident. If the user clicks the single-claim button everything continues with no problem. If the user clicks the multi-claim incident button, a combo box appears to the side with a dropdown list of MultiClaim_Incident_ID numbers that they need to select from. What I'm trying to do is if the user selects the Multi-Claim Incident button AND does not select an Incident ID number from the dropdown down list (i.e. leaves it at default value) then the rest of the form is disabled until corrected as well as clear all the fields...
It seems like it should be pretty straightforward but I can't seem to get it to work, I'm not sure if my logic is flawed or what. Here's an abridged version of my VBA code:
Private Sub Form_Load()
Me.SM_Frame.Value = 1
Me.MultiClaim_Drpdwn.Value = Null
End Sub
Private Sub SM_Frame_AfterUpdate()
If SM_Frame.Value = 1 Then
Me.MultiClaim_Incident_ID_Label.Visible = False
Me.MultiClaim_Drpdwn.Visible = False
ElseIf SM_Frame.Value = 2 Then
Me.MultiClaim_Incident_ID_Label.Visible = True
Me.MultiClaim_Drpdwn.Visible = True
ElseIf SM_Frame.Value = 2 & MultiClaim_Drpdwn.Value = Null Then
Me.Incident_Date = Null
Me.Incident_Date.Enabled = False
Me.Claimant_Name.Value = ""
Me.Claimant_Name.Enabled = False
//PATTERN CONTINUES FOR REST OF FIELDS//
MsgBox ("CLEAR EVERYTHING!!")
ElseIf SM_Frame.Value = 1 Then
Me.Incident_Date.Value = ""
Me.Incident_Date.Enabled = True
Me.Claimant_Name.Value = ""
Me.Claimant_Name.Enabled = True
//PATTERN CONTINUES FOR REST OF FIELDS//
MsgBox ("Everything can continue as is")
End If
End Sub

Let me just say that getting sequences like these right is NOT straightforward AT ALL! So don't feel bad about not getting it right on the first try. I have to create things like that about once a month and still need a lot of tries until it works in all situations.
Try to seperate to concerns:
You already have SM_Frame_AfterUpdate(). Do in it what you must to handle changing from Value 1 to 2, namely making the Combobox visible, but STOP there. You dont know what will happen to the fields with information from SM_Frame alone, you need to wait for MultiClaim_Drpdwn. Also, do what is needed to go from 2 to 1, namely hide the Combobox.
Next, create an AfterUpdate-handler MultiClaim_Drpdwn_AfterUpdate(). Use THAT to deal with the fields (enable/disable, set empty) according to its value.
Once you have that in place, you only have some edge cases remaining. For example, you want the fields to behave like MultiClaim_Drpdwn_AfterUpdate() states right after you change the SM_Frame. Thats easy once you understand that you can just happily CALL MultiClaim_Drpdwn_AfterUpdate() from within SM_Frame_AfterUpdate(), best done at the very end. These eventhandlers are still just normal functions, already public and available for anyone. This will make things chain nicely when you come from the radiobutton or not when you come from the Combobox.

In an "elseif" series, once a condition is true, the rest is ignored.
So, your
ElseIf SM_Frame.Value = 2 & MultiClaim_Drpdwn.Value = Null Then
is never reached, because you've got
ElseIf SM_Frame.Value = 2 Then
before.
In the same idea, the "ElseIf SM_Frame.Value = 1 Then" after the MsgBox is totally useless, because it's hidden by "If SM_Frame.Value = 1 Then"
Try to use the step-by-step debug mode to see that.

Related

Make Text Box visible with condition

I am new at VBA and I am trying some code and I could not find a solution or where I am doing wrong.
What I want: The combination of two different text boxes make another text box visible(so it starts in useform not visible). With one condition I could do it using select case(LLL) but when I try it with another variant it does not work(XXX). There is no error message, the code runs but does not show the text box.
Sub Visible()
If userform.TextBox5.Value = "XXX" And userform.TextBox10.Value = "245" Then
userform.TextBox1.Visible = True
userform.T_1.Visible = True
Else
GoTo LLL
End If
LLL:
Select Case True
Case userform.TextBox5.Value = "LLL", userform.TextBox10.Value >= "145"
If userform.Option000.Value = True Then
userform.TextBox1.Visible = True
userform.T_1.Visible = True
Else
userform.TextBox1.Visible = False
userform.T_1.Visible = False
End If
End Select
I understand some of your questions I must say that my code is quite big one cause it is linked to SAP in order to get some values from there and due to its size the complete code is splitted in modules and I am only sharing the module where I am facing problems.
There is one case for select case statement cause it was the only way that it worked close for what I need. I have a lot of variables for the fields TextBox5 and TextBox10 the values of these textboxes come from SAP and when I combine them, other Text Boxes shall be visible depending on the variables given. The problem is that for just one combination (this one is the one that I applied the select case statement) I need another variable (option000) so that TextBox1 and T_1 become visible. When I tried to do it only with if statements it did not worked.

Access 2016 VBA command button to sort not working

I need help alphabetically sorting a continuous list of records on a form. Clicking button SortAZ is supposed to trigger this action.
Details:
tblPatients has a list of patient names and their ID numbers. When I create a form based solely on this information, a cmd button is created and works as expected. I want to also allow the user to filter the list by any criteria (say they only remember the first name, but not the ID or last name).
frmPatients has a header above the Detail section. I can't seem to get vba to recognize the fields in the detail section. I have tried several different combinations (see commented lines) and even renaming the Detail section.
Private Sub SortAZClick()
Me.FilterOn = False
Me.OrderByOn = False
'Me.LastName.SetFocus
'Me.OrderBy = "LastName"
'Me.OrderByOn = True
'Me.Detail.OrderBy = "[LastName]"
'Me.Detail!OrderBy = "LastName"
Me.FilterResults!OrderBy = "[LastName]"
'Me.Form.OrderBy = "LastName"
Me.Form.OrderByOn = True
'me.Detail!requery
Me.Requery
End Sub
Oh the horror!!! The problem was in the first line. Should have been Private Sub SortAZ_Click() Simply leaving out the underscore prevented it from doing ANYTHING.

Using For Loop to Check for duplicates - Error The error is: Object reference not set to an instance of an object

I have a problem that I and my limited experience cannot figure out.
I have a multi-display (form) program that is for a game show system which uses a (5) display system. As I am wrapping this project up, I have been doing some house keeping and handling some potential user errors up front.
In this instance: There is a form called GameSetup which has drop down menus that are filled via a database selection of "Teams". When a Team is selected, (10) labels are populated with registered team members. The user drags and drops a team member (ie. player) to a group of (6) labels, which will make up the active team for the game.
The problem: I wanted to have a check when a name is added to the active team roster to make sure there are no duplicates. The cleanest way to do this, I thought, would be on the .TextChanged action of the active player label and use a For loop to check the other active team player names.
Private Sub lblT1_P1_TextChanged(sender As Object, e As EventArgs) Handles lblT1_P1.TextChanged
cmdReady.BackColor = Color.Yellow
For i = 1 To 6
If i = 1 Then
' Do nothing
ElseIf Me.Controls("lblT1_P" & i.ToString).Text = lblT1_P1.Text Then 'This is the line triggering the NullException
MsgBox("This Player is already selected. Please choose another Player", MsgBoxStyle.OkOnly)
Me.Controls("lblT1_P" & i.ToString).Text = "DRAG PLAYER HERE" 'This is the other line triggering the NullException
End If
Next i
End Sub
i = 1 Do Nothing is so that it wont compare against itself (in this instance)
When I run the program (de-bug), before the form GameSetup loads, I am getting a Null Exception, which makes sense as it is looking at a form or an object in a form that is not yet initialized. Clearly, the Me.Controls is the problem, but I do not know how to handle using the integer (i) in the For Loop otherwise.
The label names are lblT1_P1 - lblT1_P6 (3 instance of each group (T1/T2/T3). I'm only dealing with T1_P1 at the moment.
Any suggestion would be greatly appreciated.
Use string.Format() to generate your control names, because it is more readable.
Instead of Me.controls("lblName").Text
use DirectCast(Me.FindControl("lblName"), Label).Text

Make visible a text box based upon input of a combo box using a lookup that stores multiple values

my first ever attempt at VBA and I'm struggling with this little challenge
I can make it work when using a combo box getting the values from another table, but when I use the lookup and allow it store multiple values it gives me run-time error '13' Type mismatch
Private Sub DEPARTMENT_AfterUpdate()
If Me.DEPARTMENT = "BAR" Then
Me.test3.Visible = True
End If
End Sub
Here's the little bit of code I'm using
Can I ask is it possible? if so could someone point me in the right direction
Many thanks
If I'm understanding this clearly, you have a combobox and a textbox on a form. You want the textbox to become visible depending on what is selected from the combobox list?
If that is the case then in form load:
Private Sub Form_Load()
Me.txtboxname.Visible = False
End Sub
In combobox click event:
Private Sub comboboxname_Click()
If Me.comboboxname.ListIndex = 0 Then
Me.txtboxname.Visible = True
ElseIf Me.comboboxname.ListIndex = 1 Then
Me.txtboxname.Visible = True
Else
Me.txtboxname.Visible = False
End Sub
The numbers that correspond with the ListIndex are in order from top to bottom. 0 is typically the first selection on the combobox list. Play around with the numbers to make sure it works the way you want it to.
Hope this helps.

VB.net If statements across different forms not working

I've been encountering the same problem over and over with some IF statements. One one form I've got this:
If KarlConvo = 4 Then
MsgBox("Karl: Wow man. I overheard everything. I don't like your methods but it did work. Have this man. I can't wait until she approaches my sexy self")
MsgBox("You have obtained the SANDWICH")
PlayerProfileMale.tpbSandwich.Visible = True
PlayerProfileMale.lblSandwich.Visible = True
Form2.lblFact3C2.Visible = True
KarlConvo = 5
TobyConvo = 4
PlayerProfileMale.EatButton = True
End If
The code above happens after a msgbox pops up. It should allow for a 'Eat' button to appear on another form. The IF statement on that form is:
If EatButton = True Then
btnEat.Visible = True
End If
Well the problem is that the button is not showing on my second form even though the only condition it has is for the EatButton variable (which is a Public Property) to be true. Any ideas on why this is happening?
Thanks in advance
Here are a couple of things to check:
Show, paint, or reload the PlayerProfileMale form after you assign PlayerProfileMale.EatButton = True. Trigger an event that causes the If EatButton statement to be executed.
Make sure you're addressing the proper instance of PlayerProfileMale when you assign PlayerProfileMale.EatButton = True. This shouldn't normally be a problem, but can be if you create an instance of the form and another instance is created (or already open) when you make the assignment.