Runtime error 2482 while executing string as code - vba

So I'm attempting to change the value of a textbox on a form with VBA code.
I'm actually doing this by running a string as code so I can loop through a number of textboxes that are named similarly (RacewayOrder1, RacewayOrder2, etc...) and populate them all one at a time. So my code looks like this:
Private Sub FillRow(ByVal OrderNumber As Integer)
Dim tempstr As String
tempstr = "RacewayOrder" & OrderNumber & " = " & OrderNumber
Eval (tempstr)
End Sub
However, I am getting runtime error 2482 "Database cannot find the name 'RacewayOrder1' you entered in the expression"
I have verified that this isn't a typo, as I copied the text box's name directly into vba, but that doesn't seem to help. I even created the tempstr variable so I could inspect the code it is attempting to run, but that hasn't helped at all either.
To make matters more annoying, I debug.printed tempstr and pasted it in as a command, and it works fine.
Any advice?

You can't use Eval() to assign a value to your text box. If you overcome the RacewayOrder1 not found problem, Eval() will treat your string expression as a test for equality, not as a value assignment.
Forget about Eval() for this and simply reference the text box by name in the form's Controls collection:
Me.Controls("RacewayOrder" & OrderNumber).Value = OrderNumber

Related

Access VBA Dlookup Run-time Error - I thought I understood Dlookups lol

I am getting the following error when trying to run my code:
My code is this:
Dim ID As Long
Dim frm As Access.Form
Set frm = Forms!Frm_JobTicket
'Look up Customer Name and insert corresponding ID from Qry_CustomerID
ID = DLookup("Customer_ID", "Qry_CustomerID", "Customer_Name = " & frm("Customer_Name"))
Syteline_Customer_ID = ID
End Sub
I originally was trying to set a form control [Syteline_Customer_ID] = to the Dlookup, but it was giving me this same error, so this was my attempt at working around it. [Syteline_Customer_ID] is a text box on Frm_JobTicket.
Qry_CustomerID pulls in 2 fields from Tbl_MasterCustomerList. That table's structure is as follows:
Customer_ID - Number (Integer)
Customer_Name - Short Text
Billing_Address - Short Text
Contact_Person - Short Text
CP_Email - Short Text
CP_Phone - Short Text
I would appreciate the help, honestly I have no idea why I'm getting this error. I have already checked the Qry_CustomerID is not spelled wrong, and is not missing an underscore, that's just my naming convention. I even tried changing it to just Query1 to call it and it didn't work.
Text fields require quote enclosure around literal values. Without quotes, the Access engine assumes you are referencing another field by that literal value. And in your case the space in the name value raised a syntax error. Therefore, in concatenating the form control value, simply enclose it with single quotes:
"Customer_Name = '" & frm("Customer_Name") & "'"
Actually, you do not even need any concatenation as DLookUp can read open form fields with absolute referencing:
"Customer_Name = Forms!Frm_JobTicket!Customer_Name"

Access 2016 Subform VBA Using Double Constant as Object?

So I have a subform VBA function that looks like this:
Function GetVariable() As Double
' <control> = DLookup("<table var>","<table>", "<other table var> = <control>"
[someFunction] = DLookup("[Var]", "[tblExample]", "[tblExampleVar] = [subFormControl]")
' (return) = ( <control> - <otherControl>) / 12345.12
GetVariable = ([finalPosition] - [someFunction]) / 12345.12
End Function
And when I open the parent form (the form that contains this subform), I get the error, "Run-time error '2447' There is an invalid use of the . (dot) or ! operator or invalid parentheses."
What I gather from this is that Access is interpreting 12345.12 as an object and I do not understand why. When I run this subform on its own it works, but when it is a subform it does not. Has anyone dealt with this before?
Extra information: I have two subforms in this parent form that use the same calculation, both repeated in their form-specific VBA, and I do not think that they would conflict with one another because they do not share scope. So my conclusion remains that Access is trying to use 12345.12 as (object).member.
Thanks for reading.
Try to take care of Null values and to be more specific.
Also, a decimal value must be concatenated as a string with dot decimal separator:
Function GetVariable() As Double
If Not IsNull(Me![subFormControl].Value) Then
Me![someFunction].Value = DLookup("[Var]", "[tblExample]", "[tblExampleVar] = " & Str(Me![subFormControl].Value) & "")
If Not IsNull(Me![finalPosition].Value - Me![someFunction].Value) Then
GetVariable = (Me![finalPosition].Value - Me![someFunction].Value) / 12345.12#
End If
End If
End Function
References that work when a form opens independent won't necessarily work when that same form is used as a subform. That requires referencing to include the subform container name.
I don't see how running form as standalone could return correct data. I presume [subFormControl] is a field or control on form. This is a variable input. Variable must be concatenated in the DLookup() WHERE condition expression.
It seems these fields return names of controls or table. Do these names have spaces or punctuation/special characters (other than underscore). Really should not use in names nor use reserved words as names. If you do, need to delimit with [].
If you are referencing fields/controls on form to build a DLookup(), then all the inputs are variables and should be concatenated.
[someFunction] = DLookup("[" & [Var] & "]", "[" & [Table] & "]", "[" & [Condition] & "] = '" & [subformControl] & "'")
Whether or not delimiters are needed (and which ones) depends on the field type of the field that will be returned by the Condition input in the filter criteria.

Input box Compile Error

I have been trying to get the name of a file name when someone uses this Macro. But for some reason every time I run the Macro I get the error:
Compile error:
Wrong number of arguments or invalid property assignment
I have looked at tons of videos and other responses but none of them have helped me deal with this error.
Sub inputbox()
Dim x As Variant
x = inputbox("Please enter your file name:", "File name")
MsgBox ("Your file name is" & x)
End Sub
Here is your procedure, corrected: (Copy & paste into a new module.)
Option Explicit
Sub MyInputBoxTest()
Dim x As Variant
x = inputbox("Please enter your file name:", "File name")
MsgBox ("Your file name is " & x)
End Sub
Explanation:
Variant was misspelled. Note that since Variant is the default data type, you actually don't need to specify it. Dim x is the same as Dim x as Variant.
You can't use names like InputBox OR MsgBox as the name of your procedure. They are reserved words -- already used by another procedure built-in to VBA, so it confuses the compiler since it doesn't know which one you're referring to.
Added a space after the word is. (purely cosmetic)
Functions like MsgBox and InputBox can be called different ways depending on whether you use brackets and whether you need to return a value.
InputBox "Hi"
InputBox ("Hi")
...either of these will run fine (but won't return the what the user enters.)
Dim x
x = InputBox ("Hi")
...will return the value to variable x, however:
x = InputBox "Hi"
...will throw an error.
It's highly recommended that, especially while learning, you add the line Option Explicit to the very top of every module. This will help "force" you to properly declare and refer to variables, objects, etc, by generating compile errors when you try to compile (F9) or run (F5) the code.

Conversion of VB Variable Types

I'm experimenting with learning how conversions work between variable types. Right now, I'm looking at using one conversion inside a Try/Catch (for values that can't convert). Is there a way to have a string representation of a value (obtained from a TextBox), convert it to a test type, and then see how that converts to all the other VB standard types in a loop? Or even better if there is a resource that already does this.
I can do this, but the code is very close to being repetitive and I'm hoping for a loop of some kind to simplify and shorten it.
First of all, there is no such thing as variable type in VB.net. May be you confusing with object variables - but this is not the same thing
Dim o As Object
o = 1 ' integer
The type that is stored in o is still integer. It is boxed.
Dim i As Integer = CInt(o)
You just un-boxed it. It works because object is lowest of types and all other derive from it. So, it can "box" any other type.
In UI we use text boxes to collect data. Text boxes can contain only string. And unless you writing these strings to a file like txt or xml you usually need to convert these strings to a type you use in application.
Dim port as Integer = Convert.ToInt32(txtPort.Text)
This is not really the area where you can determine, what type is in that text box. You really need to know upfront - what are you expecting there? You can test your text for being one type or another by using
Integer.TryParse
Date.TryParse
.....TryParse
.............
But the thing is, some data can successfully pass this test fro multiple types.
Good question. While it is possible to declare variables of type type and use them in a loop, these cannot be used in declarations or DirectCast.
Dim types() As Type = {GetType(Integer), GetType(Double)}
Dim testType As Type = GetType(Double)
The easiest way might be to test each value individually something like this (although you'll probably want a try-catch for each or all the items).
Dim xInteger As Integer
xInteger = TextBox1.Text
s &= "Integer: " & xInteger.ToString & vbcrlf ' or some test
Dim xDouble As Double
xDouble = TextBox1.Text
s &= "Double" & ": " & xDouble.ToString & vbcrlf
...

Compile Error Expected Function Or Variable

I'm new and trying to learn VBA. When I'm typing in the code I get Compile Error Expected Function Or Variable.
Is something regarding the activecell, but can't figure it out.
Sub Testare()
Dim FilmName As String
Dim FilmLenght As Integer
Dim FilmDescription As String
Range("b10").Select
FilmName = ActiveCell.Value
FilmLenght = ActiveCell.Offset(0, 2).Value
If FilmLenght < 100 Then
FilmDescription = "Interesant"
Else
FilmDescription = "Suficient"
End If
MsgBox FilmName & " is " & FilmDescription
End Sub
This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a").
This gives an error that fits to your description.
Regards
It is possible to make your code fail in two different ways:
Place a very large value in D10
Place a text value in D10
This will result in either an overflow error or type mismatch error.
I know this was asked a while ago, but I ended up with the same error when I created a Sub within a worksheet with the Sub Name the same as the Worksheet name.
Unfortunately when this happens, no error is highlighted by a compile; the error only appears at run time. Also, the offending line is not highlighted, which makes it a bit difficult to find.
Changing the Sub name solves it though.
Here is what caused this error in my case:
Inside a new Public Function that I was starting to write, I began to type a statement, but needed to copy a long varname from another location, so I just inserted the letter a to prevent Excel from popping-up the uber-annoying Compile Error: Expected Expression dialog. So I had this:
myVarName = a
I then attempted to step-through my code to get to that point, and when Excel entered that function I received the message "Compile Error: Expected Function Or Variable". To resolve, I just changed the placeholder to a number:
myVarName = 1
And all continued just fine.