Compile Error: Argument Not Optional | Access VBA - vba

I have a form on Access where I have 3 ComboBoxes (Section, Rooms: White House, Rooms: Churchills). I need to have it so when 'White House' is selected from the 'Section' ComboBox, 'Rooms: Churchills will be Locked. And the opposite for 'Churchills'.
I have the code:
Private Sub Section_Change()
If Section.Value = "White House" Then
Rooms__White_House.Enabled = True
Rooms__Churchills.Enabled = False
Else: Section.Value = "Churchills"
Rooms__White_House.Enabled = False
Rooms__Churchills.Enabled = True
End If
End Sub
But when I test the form it throws the error:
Compile Error: Argument Not Optional
Any Help Appreciated. Thanks in Advance!

The problem is that Section is a reserved word. So the compiler doesn't recognise it as the name of your combobox. Just change the name of the combo to something else, e.g. cboSection, and your code will compile. (You naturally need to change your code to match the new name).
This link shows a list of names that you should avoid when working with Access http://allenbrowne.com/AppIssueBadWord.html
BTW, your line
Else: Section.Value = "Churchills"
seems a bit odd as it is setting the value of the combo. I'm guessing that you are meaning to comment what the value must be if you enter the Else section. If that's the case then you need to add the apostrophe in front of the comment, i.e.
Else: 'Section.Value = "Churchills"

Related

Error "application-defined or object-defined"

I have a form contains two subforms (BuyList_Q subform) and (ProductStore_Q subform)
I use a button to transfer data from ProductStore to BuyList
Private Sub Command69_Click()
On Error GoTo Err_AddtoOrder_Click
Me.BuyList_Q_subform.Form.BL_PCode.Value = Me.ProductStore_Q_subform.Form.BuyCode.Value
Me.BuyList_Q_subform.Form.BL_PName.Value = Me.ProductStore_Q_subform.Form.P_Name.Value
Me.BuyList_Q_subform.Form.BL_PPrice.Value = Me.ProductStore_Q_subform.Form.P_Price(S).Value
Me.BuyList_Q_subform.Form.BL_PCount.Value = Me.CountNum_txt.Value
Exit_AddtoOrder_Click:
Exit Sub
Err_AddtoOrder_Click:
MsgBox Err.Description
Resume Exit_AddtoOrder_Click
End Sub
all of this working good but this line
Me.BuyList_Q_subform.Form.BL_PPrice.Value = Me.ProductStore_Q_subform.Form.P_Price(S).Value
get the error in the title !!
Could you help me to solve this issue?
Issue is with ( ) characters in object name. Advise not to use space nor punctuation/special characters (underscore is exception) in naming convention. If you do, then must enclose in [ ] characters to define name.
Also, not necessary to use .Value because this is default property for data input controls.
Me.BuyList_Q_subform.Form.BL_PPrice = Me.ProductStore_Q_subform.Form.[P_Price(S)]
Don't do this naming and preserve your sanity.

Error in Report Builder 3.0

I have a text box named txtError on a report that I want to toggle betweem hidden and visible based on a parameter using this code:
Public Sub ShowHideUrl(ByVal param As String)
If Trim(param) = ""
txtError.Hidden = False
Else
txtError.Hidden = True
End If
End Sub
I keep getting:
There is an error on line 2 of custom code: 'txtError' is not declared. It may be anaccessible due to its protection level.
Any advice would be appreciated.
Thanks,
Donald
It shouldn't be necessary to use VBA for this, SSRS text boxes have a Visibility.Hidden property. You can set this up like so.
=IIf(Trim(Parameters!yourParameter.Value) == "", True, False)

Underline property of textbox not working like the others

I simply want to underline some text in a worksheet textbox using VBA, from a specific character to another. It should be extremely simple, and I can do it without problem with Bold and Italic.
I have the following sub
Sub ew()
Dim txt1 As Shape
Set txt1 = Sheet1.Shapes("txt_1")
txt1.TextFrame.Characters.Text = "Bold and Underline this"
txt1.TextFrame.Characters.Font.Bold = True
txt1.TextFrame.Characters.Font.Italic = True
txt1.TextFrame.Characters.Font.Underline = True
End Sub
The code fails on the last line, which is extremely strange because it worked for the 2 previous lines. the error (1004) says something like "Impossible to define the Underline function of the Font property".
To recreate the problem, take my sub to a new Excel document and create a textbox named "txt_1", that's all you need to run it.
If anyone has any idea why it fails, please help!
You need to define the Underline style. Taking your last line
txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle
Use TextFrame2 for underline
txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine

Runtime error 438 on remove dynamically added userform control

I've got the following code:
Private Sub cboA_change()
'Something to determine number of controls there should be, variable gC
'Something to determine number of controls there are, variable nC
'The first time the code runs, the following code runs:
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
'The second time the code runs, the following is executed:
For i = 7 To nC
Me.Frame1.Controls("txtGroup" & i).Remove 'ERROR HERE
Next
For i = nC to gC
frmA.Frame1.Controls.Add("txtGroup" & i)
Next
End Sub
Something like this, the code is way bigger and I tried to clear it up so if the structure doesn't seem right, that doesn't matter really.
I debugged the Add statement and I know there is a control added to the userform, called txtGroup7. However, when I later try to remove this control, I get Run-time Error 438: Object Doesn't Support This Property or Method. I tried changing the code to:
Me.Frame1.Controls.Remove ("txtGroup" & i)
But this didn't work either.
Can anybody point me in the right direction?
Edit:
I know the help says the following:
"This method deletes any control that was added at run time. However,
attempting to delete a control that was added at design time will
result in an error."
But since the control is added in run-time (dynamically, with VBA code) this shouldn't be a problem, right?
Edit 2:
I don't get why this works, but it seems to work:
q=0
While q < Me.Frame1.Controls.Count
If Me.Frame1.Controls(q).Name = "txtGroup7" Then
Me.Frame1.Controls.Remove q
Else
q = q + 1
End If
Wend
You must have something else wrong in your code, because Removeshould be working. Tried :
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
Me.Frame1.Controls.Add "Forms.TextBox.1", "Text1", True
Else
Me.Frame1.Controls.Remove "Text1"
End If
End Sub
on a UserForm with a ToggleButton and a Frame and it correctly add and remove the TextBox when pressed.

How to insert a conditional combobox?

I'm working with VBA, and I'm struggling to get a combobox with two options:
First option: the textbox next to it must appear one "-", like if is supposed to be empty or disabled.
Second option: the same texbox must must be able to receive an input, like numbers.
Like: "Do you have an ID?" if no, don't fill the texbox. If yes, fill it with your number.
Thanks!
I'm assuming C# implementation but this would work essentially for any .net or WINFORMS project.
if(cbo.selectedindex = 0)
tbFoo.text = "-";
else if(cbo.selectedindex == 1)
tbFoo.text = "filltextwithID";
Check if the selectedindex of your combobox is the first or second options in the list and do your first option with the first selectedindex, else if it is the second, fill your textbox with whatever you need to fill it with(textwise).
Use your conditional if statement(naturally, I know) to check your conditions that you want your combobox to do to your textbox.
Otherwise another way to do this is with a selectedindexchanged event and do your switch statement or if statement based on which selectedindex you are talking about. 0 is the first item....all the way up to n items.
I got it! Here's the code:
Private Sub ComboBox_Change()
If ComboBox = "I don't have an ID" Then
IdTextBox.Visible = False 'Hidden
IdLabel.Visible = False
Else
IdTextBox.Visible = True 'Unhidden
IdLabel.Visible = True
End If
End Sub