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.
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.
This is very easy to replicate....Please follow my steps with MS Word.
In a Word file, create a fairly large table (of about five columns and twenty rows). Populate all cells with some junk text.
Or, pick a "good sized" table in a file that you already have.
Select (or "Highlight") a few rows of the table, such as three rows.
Go to the Font section of the Word Ribbon. Notice that the Hidden
property of the selection is showing as "not checked" which means
visible. This is what you expect to see (for now).
Optionally, also go to the Immediate Window of the VB Editor and
type "? Selection.Range.Font.Hidden". Word will respond "0", meaning
the .Hidden property is false. This is another way of verifying what
you saw in Step 2.
Now select an additional few rows of the table. Go to Step 2 and repeat.
By the time your Selection gets up to about 12 rows (your results may vary somewhat), the Hidden checkbox shows a solid block to indicate "mixed hidden-ness", and "? Selection.Range.Font.Hidden" shows 9999999 to indicate 'undefined" state.
WHY does the .Hidden property become unreliable/unreadable when the selection is in a table and the selection is relatively many rows? This does not happen when the Selection in a non-table. This is messing up a program of mine that is otherwise very good.
Thanks to all who puzzled over this bug in MS Word. My workaround is as follows. I make the assumption that the hidden-ness (or other property of the selection like .Font or .Underline) of the the selection is "uniform" ... that I can try to read the property of the first 10 characters rather than the entire selection, and still get the right answer. If you do not know that your entire selection has the same properties as the first 10 characters of your selection, this solution won't help you.
Function DigDeeperInto(fullSelection As Selection) As String
' This returns the truth of the hidden-ness of the *front end* of a Selection.
' This is used because Word sometimes mis-reads the hidden-ness of a whole Selection.
Dim shortRange As Range ' will hold the just the front end of the full Selection
Dim theTruth As String
Set shortRange = fullSelection.Range
shortRange.SetRange Start:=shortRange.Start + 1, End:=shortRange.Start + 10
If shortRange.Font.Hidden = wdUndefined Then theTruth = "UnDef" ' this Function failed
If shortRange.Font.Hidden = 0 Then theTruth = "Visible"
If shortRange.Font.Hidden = -1 Then theTruth = "Hidden"
DigDeeperInto = theTruth
End Function
After selecting a Selection, and assuming that Selection.Font.Hidden will not be reported correctly, I call the DigDeeperInto(Selection) function. It looks at only the first 10 characters and returns a string "Visible", "Hidden", or "UnDef". If it still returns "UnDef" (which does not happen for me, luckily), that would indicate DigDeeperInto() failed to solve the problem.
For what it's worth this is not specific to tables; the exact same thing happens in a document that has 51 blank (but otherwise identical) paragraphs. If you select 50 paragraphs you can get real results for:
?selection.Range.Paragraphs.Count ' just to confirm the selection
?selection.Font.name
?selection.Range.Font.Hidden
BUT as soon as you select the 51st paragraph things lose definition, so to speak.
I imagine that looping over your table row by row (as suggested by #jsotola) would be a good workaround.
To answer the question as to WHY this happens, well the best anyone outside of MS can do (I think) is speculate. If I were to speculate I would suggest that there is some sort of internal limit or limitation when it comes to processing or storing detailed information for so many paragraphs. Is it a bug, I think so (but that is just IMHO).
Function to replace the buggy built-in function
Example of a function that will return True, False or wdUndefined (9999999) depending on whether font is Hidden, Visible or a mixture of Hidden & Visible (i.e. undefined), respectively.
Note that it is entirely possible for wdUndefined to be a valid result as an otherwise visible paragraph may have a single character hidden by font and therefore that paragraph's font would neither be entirely visible, not hidden - so it is 'undefined' - hence wdUndefined is very valid.
Example usage and output:
?selection.Paragraphs.Count
2016
?fontIsHidden '(automatically operates on the active selection)
False
The function:
' Put this code into a STANDARD module
Function fontIsHidden() As Variant ' Variant to return True, False, wdUndefined
Dim paraSelected As Paragraph
Dim NotFirstPara As Boolean
Dim result As String
For Each paraSelected In Selection.Range.Paragraphs
' This could be done with Table.Rows instead of Paragraps
' (but would error if there was no table)
If NotFirstPara Then
If paraSelected.Previous.Range.Font.Hidden <> paraSelected.Range.Font.Hidden Then
result = wdUndefined ' this para is different to the previous paras
Exit For
End If
Else
' Setup for the first paragraph
Select Case paraSelected.Range.Font.Hidden
Case False ' 0
result = False
Case True ' -1
result = True
Case wdUndefined
result = "Undefined" ' some text in the para is visible and some is hidden
End Select
NotFirstPara = True
End If
Next paraSelected
fontIsHidden = result
End Function
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.
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.
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.