Special Characters in variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there any way to use special characters in variables? I've inherited a
database with messed up field names and I want to use a field name with a /
in it as a variable. Is there a way to do this without renaming the field?
 
LaRae said:
Is there any way to use special characters in variables? I've
inherited a database with messed up field names and I want to use a
field name with a / in it as a variable. Is there a way to do this
without renaming the field?

I don't think you can use field names as variables. I think you can
refer to fields in a recordset, for instance like this

rs![my/crazyname]
rs("my/crazyname")
rs.fields("my/crazyname")
dim strFieldName as String
strFieldName = "my/crazyname"
rs.fields(strFieldName)

etc, and form controls the same way, just substitute rs with Me or the
more qualified Forms!frmFormName / Forms("frmFormName")

But do please tell what you intend to do, then we can probably assist.
 
Here is the code that I am using:

Private Sub Short_Summary_BeforeUpdate(Cancel As Integer)
If Invoice/Contract# = DLookup("Invoice/Contract#", "Contractual_table",
"[Invoice/Contract#] = '" & Short_Summary & "'") Then
MsgBox "That contract number is already in use."
Cancel = True
Me.Short_Summary.SelStart = 0
Me.Short_Summary.SelLength = Len(Me.Short_Summary)
End If
End Sub

When it is compiled, VB puts a space on either side of the / on the first
invoice/contract#.

RoyVidar said:
LaRae said:
Is there any way to use special characters in variables? I've
inherited a database with messed up field names and I want to use a
field name with a / in it as a variable. Is there a way to do this
without renaming the field?

I don't think you can use field names as variables. I think you can
refer to fields in a recordset, for instance like this

rs![my/crazyname]
rs("my/crazyname")
rs.fields("my/crazyname")
dim strFieldName as String
strFieldName = "my/crazyname"
rs.fields(strFieldName)

etc, and form controls the same way, just substitute rs with Me or the
more qualified Forms!frmFormName / Forms("frmFormName")

But do please tell what you intend to do, then we can probably assist.
 
It's not possible. From the Help file:

Visual Basic Naming Rules

Use the following rules when you name procedures, constants, variables, and
arguments in a Visual Basic module:

- You must use a letter as the first character.
- You can't use a space, period (.), exclamation mark (!), or the characters
@, &, $, # in the name.
- Name can't exceed 255 characters in length.
- Generally, you shouldn't use any names that are the same as the functions,
statements, and methods in Visual Basic. You end up shadowing the same
keywords in the language. To use an intrinsic language function, statement,
or method that conflicts with an assigned name, you must explicitly identify
it. Precede the intrinsic function, statement, or method name with the name
of the associated type library. For example, if you have a variable called
Left, you can only invoke the Left function using VBA.Left.
- You can't repeat names within the same level of scope. For example, you
can't declare two variables named age within the same procedure. However,
you can declare a private variable named age and a procedure-level variable
named age within the same module.

Note: Visual Basic isn't case-sensitive, but it preserves the capitalization
in the statement where the name is declared.



On the other hand, I don't see why you need to have the variable named the
same as the messed-up field name.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


LaRae said:
Here is the code that I am using:

Private Sub Short_Summary_BeforeUpdate(Cancel As Integer)
If Invoice/Contract# = DLookup("Invoice/Contract#", "Contractual_table",
"[Invoice/Contract#] = '" & Short_Summary & "'") Then
MsgBox "That contract number is already in use."
Cancel = True
Me.Short_Summary.SelStart = 0
Me.Short_Summary.SelLength = Len(Me.Short_Summary)
End If
End Sub

When it is compiled, VB puts a space on either side of the / on the first
invoice/contract#.

RoyVidar said:
LaRae said:
Is there any way to use special characters in variables? I've
inherited a database with messed up field names and I want to use a
field name with a / in it as a variable. Is there a way to do this
without renaming the field?

I don't think you can use field names as variables. I think you can
refer to fields in a recordset, for instance like this

rs![my/crazyname]
rs("my/crazyname")
rs.fields("my/crazyname")
dim strFieldName as String
strFieldName = "my/crazyname"
rs.fields(strFieldName)

etc, and form controls the same way, just substitute rs with Me or the
more qualified Forms!frmFormName / Forms("frmFormName")

But do please tell what you intend to do, then we can probably assist.
 
Should probably have included the fact that despite the fact they're not
explicitly mentioned in the rules, you also can't use +, -, / or * in
variable names. That's because VBA will interpret them as arithmetic
operations (which is why the spaces were put in when you typed it).

Incidentally, the reason the characters @, &, $, # and ! aren't accepted is
because VBA allows you declare variable type by appending a single character
type-declarationg character: Dim x$ and Dim x As String produce the same
results. @ is Currency, & is Long, $ is String, # is Double, ! is Single and
% is Integer.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Douglas J. Steele said:
It's not possible. From the Help file:

Visual Basic Naming Rules

Use the following rules when you name procedures, constants, variables,
and arguments in a Visual Basic module:

- You must use a letter as the first character.
- You can't use a space, period (.), exclamation mark (!), or the
characters @, &, $, # in the name.
- Name can't exceed 255 characters in length.
- Generally, you shouldn't use any names that are the same as the
functions, statements, and methods in Visual Basic. You end up shadowing
the same keywords in the language. To use an intrinsic language function,
statement, or method that conflicts with an assigned name, you must
explicitly identify it. Precede the intrinsic function, statement, or
method name with the name of the associated type library. For example, if
you have a variable called Left, you can only invoke the Left function
using VBA.Left.
- You can't repeat names within the same level of scope. For example, you
can't declare two variables named age within the same procedure. However,
you can declare a private variable named age and a procedure-level
variable named age within the same module.

Note: Visual Basic isn't case-sensitive, but it preserves the
capitalization in the statement where the name is declared.



On the other hand, I don't see why you need to have the variable named the
same as the messed-up field name.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


LaRae said:
Here is the code that I am using:

Private Sub Short_Summary_BeforeUpdate(Cancel As Integer)
If Invoice/Contract# = DLookup("Invoice/Contract#", "Contractual_table",
"[Invoice/Contract#] = '" & Short_Summary & "'") Then
MsgBox "That contract number is already in use."
Cancel = True
Me.Short_Summary.SelStart = 0
Me.Short_Summary.SelLength = Len(Me.Short_Summary)
End If
End Sub

When it is compiled, VB puts a space on either side of the / on the first
invoice/contract#.

RoyVidar said:
<[email protected]>:
Is there any way to use special characters in variables? I've
inherited a database with messed up field names and I want to use a
field name with a / in it as a variable. Is there a way to do this
without renaming the field?

I don't think you can use field names as variables. I think you can
refer to fields in a recordset, for instance like this

rs![my/crazyname]
rs("my/crazyname")
rs.fields("my/crazyname")
dim strFieldName as String
strFieldName = "my/crazyname"
rs.fields(strFieldName)

etc, and form controls the same way, just substitute rs with Me or the
more qualified Forms!frmFormName / Forms("frmFormName")

But do please tell what you intend to do, then we can probably assist.
 
LaRae said:
Here is the code that I am using:

Private Sub Short_Summary_BeforeUpdate(Cancel As Integer)
If Invoice/Contract# = DLookup("Invoice/Contract#",
"Contractual_table", "[Invoice/Contract#] = '" & Short_Summary &
"'") Then MsgBox "That contract number is already in use."
Cancel = True
Me.Short_Summary.SelStart = 0
Me.Short_Summary.SelLength = Len(Me.Short_Summary)
End If
End Sub

When it is compiled, VB puts a space on either side of the / on the
first invoice/contract#.

RoyVidar said:
LaRae said:
Is there any way to use special characters in variables? I've
inherited a database with messed up field names and I want to use
a field name with a / in it as a variable. Is there a way to do
this without renaming the field?

I don't think you can use field names as variables. I think you can
refer to fields in a recordset, for instance like this

rs![my/crazyname]
rs("my/crazyname")
rs.fields("my/crazyname")
dim strFieldName as String
strFieldName = "my/crazyname"
rs.fields(strFieldName)

etc, and form controls the same way, just substitute rs with Me or
the more qualified Forms!frmFormName / Forms("frmFormName")

But do please tell what you intend to do, then we can probably
assist.

Did you try

If Me![Invoice/Contract#] = DLookup("[Invoice/Contract#]", _
"Contractual_table", "[Invoice/Contract#] = '" & _
Short_Summary & "'") Then
MsgBox "That contract number is already in use."

Sometimes when using special characters or spaces as part of the object
name, you can work around the issue by [bracketing] the object name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top