Error "application-defined or object-defined" - vba

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.

Related

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)

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.

Compile Error: Argument Not Optional | Access 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"

VBA Excel "Variables are required"

Got a trouble in a VBA excel program.
Sub code(s)
...
code = t
End Sub
And then :
Sub CommandButton1_Click()
...
For i = 0 To size
current_y = code(string_array(i))
...
End Sub
When i run the program, i got this error "Variables are required" (not sure, i'm working on a japanese version of excel). Sub CommandButton1_Click is highlighted and code is selected inside CommandButton1_Click. Can't figure out why, though it must be simple...
You're trying to return a result from a Sub. Try declaring it as a function instead, as this is able to return values to the caller:
Function code(s)
...
code = t
End Function
If it makes it any clearer, on my English version the error message is:
Expected Function or variable
Does the code include Option Explicit? Perhaps the error translates to "Variable declaration required"? Try removing option explicit - if that fixes it remove that line, then go through checking all variables are declare (e.g. dim current_y as string).

vba: passing a variable into error handles

i have a statement:
on error go to label
however i would like to pass into the label the variable which caused the error
is this possible?
You can use Err to get the error No and Description
Sub|Function SomeName()
On Error GoTo Err_SomeName ' Initialize error handling.
' Code to do something here.
Exit_SomeName: ' Label to resume after error.
Exit Sub|Function ' Exit before error handler.
Err_SomeName: ' Label to jump to on error.
MsgBox Err.Number & Err.Description ' Place error handling here.
Resume Exit_SomeName ' Pick up again and quit.
End Sub|Function
First, I think you mean:
on error goto label
And no, you can't pass variables using a goto command. However, you can check the Err.Description for details, and if you are raising your own errors, you can do this:
' Raise a custom error.
Err.Raise Number:=vbObjectError + 1000, _
Source:="TestRaiseCustomError", _
Description:="My custom error description."
So if you are raising your own error, you could set Source to the field that caused the problem.
Refer to the Use the Err Object's Raise Method to Raise Custom Errors section at this link for more info.
I can't think of a clever way to do it. I normally have an error handeling class/function that way I can use "on error goto" to pass the error to the bottom block then call the error handeling function. The advantage of this is it's nice to have a centralised error handler but also you can customise it so in my case I pass the name of the procedure thats crashed. It's not pretty but you could if you really wanted to pass either a collection of variables (dependant on how many you have) or set up something to identify the variable based on the line number (which you'd have to add manauly...)
on error goto err
'Code
err:
ErrorHandeler err, "String with Procedure name"
Declare global variables and use them in the code and in your error code.
Public global_variable1 As Integer
Public global_variable2 As String
Private Sub Btn1234_Click()
....
end sub
Err_abcd:
....
End Sub