473,394 Members | 1,759 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.

INSERT using a variable

I'm having a bear of a time here. I have a table that I want the user to be able to insert new data into (this new data will only be 1 letter). Anyway I've made a small query that accepts an @variable. When I run the query I get an input box asking me for the variable and inserts my data into the table like it should.

Where my problem arises is I need to be able to let the user click a button, enter the desired data into an input box, then update the listbox that contains the data including the new one the user just entered. I originally wanted to update 2 fields in that table by having another inputbox pop up after the 1st was entered. Figured that'd be next to impossible though.

For the life of me I can't figure out how to send the input box data to the listbox via VBA. Can anyone guide me on this please?

This is my query code...
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblIndicator ( [Indicator] )
  2. SELECT [@Indicator] AS Expr1;
Apr 26 '09 #1
9 2682
mshmyob
904 Expert 512MB
Try doing a requery of the ListBox after data has been entered in the table.

Expand|Select|Wrap|Line Numbers
  1. Me.lstBoxName.Requery
  2.  
cheers,
Apr 26 '09 #2
The ReQuery command will be the last line of my code. I can't figure out all the lines that would come before that. How do I accept a value from the form via an input box, then pass that variable to a query, then lastly I will update the listbox via the ReQuery command.

In a nutshell how would I get the data from the form to the table?
Apr 26 '09 #3
NeoPa
32,556 Expert Mod 16PB
You talk about various levels without being clear as to what exactly you're after. Are you wanting to use the InputBox() function to provide data that you subsequently wish to insert into a table? Is the ListBox control simply a reflection of this table? If so, then it's a bit of a red-herring and only confuses the question. If not, then your question is even less clear and you will need to clarify what it is you are asking.

Assuming I have your requirement sussed, you would create a SQL string within your VBA code, which includes the value just grabbed from the InputBox() function. The format of the SQL string would be, assuming the value entered were a string BLOB :
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tblIndicator ([Indicator])
  2. VALUES ('BLOB')
I assume you have the understanding of how to create such a string (We can help otherwise of course).
In case the value is not a string, see Quotes (') and Double-Quotes (") - Where and When to use them.
Apr 26 '09 #4
NeoPa
32,556 Expert Mod 16PB
@FooFighter
I should have read this again after I'd worked out what I thought you were asking. I was so struggling to understand what you were saying I overlooked this clear bit of the question.

Here is some example code for how you would get a value into the said string using VBA :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String, strValue As String
  2.  
  3. strSQL = "INSERT INTO tblIndicator ([Indicator]) " & _
  4.          "VALUES ('%V')"
  5. strValue = InputBox("Please enter Indicator value :")
  6. strSQL = Replace(strSQL, "%V", strValue)
  7. Call DoCmd.RunSQL(strSQL)
  8. Call Me.SomeListBox.Requery
PS. I should add that your form of the SQL is equally valid and will work perfectly well. It is possibly more standard to use the VALUES version when dealing solely with provided values, but either works fine.
Apr 26 '09 #5
Actually I figured out another way to do it with the following code. If anyone else is trying to do something like this here's a nice easy way.

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String 'String for the INSERT SQL statement
  2.     Dim strInputIndicator As String 'String to store the indicator letter
  3.     Dim strInputDesc As String 'String to store the Description of the indicator
  4.  
  5.     DoCmd.SetWarnings False 'Don't display the warning about inserting a row
  6.  
  7.     strInputIndicator = InputBox("Enter a one letter code for the new indicator", "New Indicator Code") 'Ask the user for code
  8.     strInputDesc = InputBox("Enter a short description for the new indicator", "New Indicator Description") 'Ask the user for description
  9.  
  10.     strSQL = "INSERT INTO tblIndicator ( [Indicator], [Description] ) VALUES (" & strInputIndicator & "," & strInputDesc & ");"
  11.  
  12.     DoCmd.RunSQL strSQL 'Run the INSERT query
  13.  
  14.     lstIndicators.Requery 'Update the list box to show new indicator
  15.  
  16.     DoCmd.SetWarnings True 'Reenable warnings
  17.  
Apr 26 '09 #6
NeoPa
32,556 Expert Mod 16PB
Have you tried running this code?

I would expect to see a run-time error on line #10. You give no indicator of the data type of your field Indicator, but from the prompt in line #8, it seems clear the field Description is a string. A string literal (not reference or variable) must be enclosed in quotes (See link in post #4).
Apr 27 '09 #7
Yeah I was getting an error, but I managed to fix it and get the thing working. Thanks for taking a peek over my code though :)
Apr 28 '09 #8
NeoPa
32,556 Expert Mod 16PB
Good for you.

Can I take that to mean the whole problem is resolved now, or does that refer just to that line of code being ok now?
Apr 28 '09 #9
The entire question is solved :)
Apr 29 '09 #10

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
18
by: Robin Lawrie | last post by:
Hi again, another problem! I've moved from an Access database to SQL server and am now having trouble inserting dates and times into seperate fields. I'm using ASP and the code below to get the...
1
by: Dark | last post by:
Im trying to insert a clob into the database and I get the following error. DBD::DB2::st execute failed: SQL0804N The application program input parameters for the current request are not...
3
by: Kelvin | last post by:
Is it possible when coding a SQL statement to use variables within an INSERT INTO statement i.e the variables contain some data I wish to insert along with the standard entity.attribute data? ...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
2
by: Stuart | last post by:
Hi there I am using the following to take an account number from a datagrid in to another page and use it to load specific details according to the account number: Public Sub...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
11
by: Eugene Anthony | last post by:
The code bellow functions well when I attemp to delete a record. But when I attemp to insert a record I am getting the following error -2147217900:Syntax error in INSERT INTO statement. How do I...
2
by: yeap | last post by:
Hi All, I can't insert java variable into ms access database. I'm using odbc connection to ms access. Below are my coding. try { ...
1
by: Maklar60 | last post by:
I am attempting to execute an INSERT statement on my page but continually get the following error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax near '<'. ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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.