473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting a string variable using another string variable

gnawoncents
214 100+
I don't know if this is even possible, but I've been searching for a while now with no success. I have a range of integer variables str1, str2, str3, etc. and need to increment their respective value by one (1) when the value of a second string (varNUM) equals the number in the first string.

Here is how I'm setting it up
Expand|Select|Wrap|Line Numbers
  1. Dim nstr1 As Integer
  2. Dim nstr2 As Integer
  3. Dim nstr3 As Integer
  4. Dim varNUM As Integer
  5.  
  6. nstr1 = 0
  7. nstr2 = 0
  8. nstr3 = 0
  9.  
  10. varNUM = DLookup("[FieldValue]", "TBLSurveySelections", "[StoreValue] = '" & fldReport & "'") 'this will set varNUM to a value of 1-3
  11.  
  12. nstr(varNUM) = nstr(varNUM) + 1 'this is the part I can't get to work
  13.  
The last line is where I can't figure out the correct syntax to get VBA to recognize it as a string variable.

What I'm hoping for is if the value of varNUM is 2, then the code will increment nstr2 by 1. Can anyone help me with the proper syntax?
May 31 '10 #1

✓ answered by ADezii

@gnawoncents
First of all, it is a reaaaaaally bad idea to preface an Integer Variable with str, but that being said:
Expand|Select|Wrap|Line Numbers
  1. varNum = DLookup("[FieldValue]", "TBLSurveySelections", "[StoreValue] = '" & _
  2.          fldReport & "'") 'this will set varNUM to a value of 1-3
  3.  
  4. 'varNum will be either 1, 2, or 3, so Increment corresponding Variable
  5. Select Case varNum
  6.   Case 1        'varNum = 1
  7.     str1 = str1 + 1
  8.   Case 2        'varNum = 2
  9.     str2 = str2 + 1
  10.   Case 3        'varNum = 3
  11.     str3 = str3 + 1
  12.   Case Else     'should not happen?
  13. End Select

8 3160
ADezii
8,834 Expert 8TB
@gnawoncents
I am really confused at to your request, but to Increment a Numeric Value stored as a String, then to convert it back to a String again:
Expand|Select|Wrap|Line Numbers
  1. CStr(Val("123") + 1) ==> 124
May 31 '10 #2
gnawoncents
214 100+
Sorry, I'll try and be more clear. I have no problem with incrementing the value. What I need to know how to do (or if it's even possible) is select which variable to increment by using another variable. In other words:

nstr1 or nstr2 or nstr3

Select which one based on another variable varNUM which could be 1 or 2 or 3

Consequently, I need to know the syntax for the string. I have tried the following, all of which I know are wrong:

nstr(varNUM)
nstr.varNUM
[nstr](varNUM)
etc.

So if the value of varNUM is 1, then when I say nstr(varNUM) + 1, VBA should read it as nstr1 + 1.

I hope this is somewhat clearer. Thank you for your help.
May 31 '10 #3
ADezii
8,834 Expert 8TB
@gnawoncents
First of all, it is a reaaaaaally bad idea to preface an Integer Variable with str, but that being said:
Expand|Select|Wrap|Line Numbers
  1. varNum = DLookup("[FieldValue]", "TBLSurveySelections", "[StoreValue] = '" & _
  2.          fldReport & "'") 'this will set varNUM to a value of 1-3
  3.  
  4. 'varNum will be either 1, 2, or 3, so Increment corresponding Variable
  5. Select Case varNum
  6.   Case 1        'varNum = 1
  7.     str1 = str1 + 1
  8.   Case 2        'varNum = 2
  9.     str2 = str2 + 1
  10.   Case 3        'varNum = 3
  11.     str3 = str3 + 1
  12.   Case Else     'should not happen?
  13. End Select
May 31 '10 #4
gnawoncents
214 100+
ADezii,

Thanks, I'll take your advice and change the str (to ... what int?) to something else.

Also, I was oversimplifying the fact when I said there were only three possibilities, which is why I was looking for an option for it to change dynamically for whatever varNum happened to be. However, if this is not possible, I will kindly use your Select Case example. Again, thank you.
May 31 '10 #5
OldBirdman
675 512MB
The original names of nstr1, nstr2, nstr3, etc. has led you into a mental trap. These are unique names to Access, and might as well have been nstr1st, nstr2nd, nstr3rd, etc. or nstrFirst, nstrSecond, nstrThird, etc. In which case, ADezii's use of the Select Case is correct.

You could declare nstr as an array.
Expand|Select|Wrap|Line Numbers
  1. Dim nstr(3) as Integer
  2. 'Initialize array to zero
  3. For varNUM = 1 to 3 'or 1 to UBound(nst)
  4. nstr(varNUM) = 0
  5. Next varNUM 
  6. ...
  7. nstr(varNUM) = nstr(varNUM) + 1
  8.  
NOTE: I have used your variable names from Post #1, but am in 100% agreement with ADezii that these names are inadvisable. I see you plan to change them.
May 31 '10 #6
ADezii
8,834 Expert 8TB
I do believe that I have a rather Unique Solution to your dilemma, whereby the dynamically created Variable Names (str1 to str9999...) and their associated Values are stored in a Table named tblValues. The code will first check and see if the Variable Name exists (str & CStr(varNum)). If the Variable Name exists, it will Increment its Value by 1, and if it doesn't exists, it will Append the Variable Name (str & CStr(varNum)) and its Value (varNum) to tblValues.
  1. Create a Table named tblValues with only 2 Fields:
    • VarName {TEXT}
    • Value (LONG}
  2. Insert the following Code Segment in its appropriate place.
    Expand|Select|Wrap|Line Numbers
    1. varNum = DLookup("[FieldValue]", "TBLSurveySelections", "[StoreValue] = '" & _
    2.          fldReport & "'") 'this will set varNUM to a value of 1-3
    3.  
    4. If DCount("*", "tblValues", "[VarName] = 'str" & CStr(varNum) & "'") = 0 Then
    5.   'Does not exist, so create the Variable Name, then Append it and the Value to the Table
    6.   CurrentDb.Execute "INSERT INTO tblValues ([VarName],[Value]) Values('str" & _
    7.                      CStr(varNum) & "'," & varNum & ")", dbFailOnError
    8. Else
    9.   'Variable Name exists, so just Update the Value Field (Increment by +1)
    10.   CurrentDb.Execute "UPDATE tblValues SET [Value] = [Value] + 1 WHERE [VarName] = 'str" & _
    11.                      CStr(varNum) & "'", dbFailOnError
    12. End If
  3. A simple DCount() and/or DLookup() can see if a Variable exists, and if it does, retrieve its Value.
May 31 '10 #7
gnawoncents
214 100+
Thank you both. I'll give these a shot and let you know.
May 31 '10 #8
gnawoncents
214 100+
Thanks for all the advice and help. I changed the integers to int (is this the appropriate terminology?) and decided to go with the ADezii's Case example for now. It took some time, but works great so far. Thanks again all.
Jun 1 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
8
by: John | last post by:
How can I set a variable (Table_Select) equal to another string variable (Table_1)? Example: -------------------------------- Table_1 = "Winner" For i = 1 to 3 Table_Select = "Table_" & i...
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
1
by: Epson Barnett | last post by:
Hi, I'm new to C# and I'd like to be able to reference a field of an object using a variable instead of literal text. In the PHP scripting language, you can create a variable: $var = "name";...
2
by: Jeff Higgins | last post by:
I would like to use a string variable to declare a variable. How would I accomplish this goal? string nameMainMenu = "mnuMain"; string nameMenuItem = "mnuFile"; MainMenu {nameMainMenu} = new...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
4
by: Emilio | last post by:
Question about Shared Sub Connect(server As , message As ) Why is in square brackets? Is it like Shared Sub Connect(server() As String, message() As String)
3
by: yogi_bear_79 | last post by:
I'm sure I have a few things wrong here. But I am stuck on how to do a recurring search. Also my statement cin >quote; acts weird. If I enter more than one word it blows right past cin >findMe;...
9
by: Slickuser | last post by:
Hi, I'm trying to use my variable in another variable. How can I achieve that? I can use + (concat) but it will be a lot me to concatenate. I have to use 3 variables with 15 replacement. ...
2
by: selvialagar | last post by:
Let me tell my problem.There are list of parameters in a list box. User can select any number of Parameters (Maximum Limit is 10). Now I want to create dynamic arrays for all the parameters....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.