473,394 Members | 1,866 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,394 software developers and data experts.

Variable retaining value from InputBox

22
I am importing Excel files into Access and trying to automate queries in order to manipulate and create reports. In a subroutine, several queries are run to CREATE and UPDATE a TEMP table.

In the beginning of a subroutine the user is asked for a variable, MyValue. The code is:
Expand|Select|Wrap|Line Numbers
  1.    'Dim MyValue As Variant
  2.    MyValue = InputBox("1. Enter Sheet #", "MyInputbox")
  3.    tblname = "Sheet" & MyValue
  4.  
Later in the same subroutine the variable MyValue is used in an UPDATE query. The code is:
Expand|Select|Wrap|Line Numbers
  1.    strSql = "UPDATE [" & tblname & "] SET MyValue1 = MyValue WHERE MyValue1 Is Null"
  2.    DoCmd.RunSQL strSql
  3.  
Why is Access putting a box up before the UPDATE query? The box indicates 'enter parameter' and asks 'Enter MyValue'?
Thanks,
Carl
Apr 18 '12 #1

✓ answered by NeoPa

Try using :
Expand|Select|Wrap|Line Numbers
  1. strSql = "UPDATE [" & tblname & "] SET [MyValue1] = " & MyValue & " WHERE MyValue1 Is Null"
This is assuming your SQL syntax was correct and that MyValue is a numeric value. If it's a string then the SQL syntax is different and your VBA should then be :
Expand|Select|Wrap|Line Numbers
  1. strSql = "UPDATE [" & tblname & "] SET [MyValue1] = '" & MyValue & "' WHERE MyValue1 Is Null"

5 4818
NeoPa
32,556 Expert Mod 16PB
Because MyValue, as defined in the first set of code is a VBA variable that has scope within that module only (according to what you include, which is unfortunately inadequate for the question.), but you are attempting to access it from within the Jet SQL engine. This has no concept of any variables defined within VBA so asks for you to provide a value for it to use.
Apr 18 '12 #2
Carl23
22
Another interesting item occurs when I add the following code just prior to the UPDATE query:
Expand|Select|Wrap|Line Numbers
  1. MyValue = InputBox("1. Enter Sheet #", "MyInputbox")
.
I find it interesting that the same box comes up and indicates 'enter parameter' and asks 'Enter MyValue'? Is there a method of avoiding answering the MyValue question.
Thanks,
Carl
Apr 18 '12 #3
NeoPa
32,556 Expert Mod 16PB
Try using :
Expand|Select|Wrap|Line Numbers
  1. strSql = "UPDATE [" & tblname & "] SET [MyValue1] = " & MyValue & " WHERE MyValue1 Is Null"
This is assuming your SQL syntax was correct and that MyValue is a numeric value. If it's a string then the SQL syntax is different and your VBA should then be :
Expand|Select|Wrap|Line Numbers
  1. strSql = "UPDATE [" & tblname & "] SET [MyValue1] = '" & MyValue & "' WHERE MyValue1 Is Null"
Apr 18 '12 #4
Carl23
22
Thanks for your help. The code worked!!!!! It seems that your answer presents the variable as a variable with expanded wildcard type information. If there is an article on this, please direct me to it. Your answer should be marked as best response but I do not see the button on my screen.
Thanks again,
Carl23
Apr 22 '12 #5
NeoPa
32,556 Expert Mod 16PB
It doesn't pertain to wildcards, but to VBA code Carl.

What you need to do to understand this is appreciate that there are two sections to it :
  1. Formulate a SQL string in VBA. Although this may contain SQL commands, at this point it is simply a VBA string. Because you're using VBA you can add items to this string from anything which is available to the VBA. VBA variables, for instance, are obviously accessible to VBA, but would not be accessible to the Jet SQL engine. We can use VBA though, to formulate a string with literals in it to represent values. Have a look at Quotes (') and Double-Quotes (") - Where and When to use them.
  2. Send the resultant string (which at this stage is simply a string value available to VBA - typically stored in a VBA variable) to the Jet SQL engine for the contents to be interpreted by that.

PS. I guess you found the Best Answer button. Thank you :-)
Apr 22 '12 #6

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

Similar topics

2
by: Remco Groot Beumer | last post by:
Hello, I created a program in which I use modules and classmodules for setting my variables. For example when I need to set the customerID in a variable I use something like: ...
6
by: Newbie | last post by:
hi all, in a webform's codebehind, i have a variable (ie: nTotal). and in UI i have a hidden htmlinput (ie: fld1). Q: how to assign the value of nTotal to now i want to assign the value of...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
2
by: php_Boi | last post by:
i have designed an application that is a dynamic submission form. i have text fields and listboxes. now i am able to retain the values of the listboxes when i populate the listbox "manually"(single...
1
by: satish2112 | last post by:
Hi, I have a text-area which contains values from mysql database and 2 buttons, Edit and Update. When I click on the Edit button, I can edit the text-area (initially non-editable). After this,...
6
by: satish2112 | last post by:
how can i pass a variable's value from javascript to perl script? how can i receive that value in the perl script? please help me!!!
2
by: satish2112 | last post by:
how can i pass a variable's value from javascript to perl script? how can i receive that value in perl script? Please help me!!!
4
by: raaman rai | last post by:
Please kindly tell me how to retain a value in the drop down box. I mean if i have a form and i select one option from the drop down box, and when i submit the form, if something is wrong in the...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: spoonybard | last post by:
I have an ASP.Net 3.5 VB web application that has a jQuery UI Tab control on it. I am trying to maintain the selected tab through postbacks so I can reselect that tab when the page reloads. In order...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.