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

UPDATE statement where table name is based on unbound textbox (VBA/Access)

bre1603
39
I'm trying to run an UPDATE statement on a table that changes every time the user runs the code.

Here's some info:
(On a form) The user imports a text file, using a button to open a file browser window and another button import to file after it's been selected. An unbound textbox requires the user to name the table before importing. This table name control (tb_Name) is used when exporting by a third button, which exports the file back to text while making some minor format changes.

The big thing I need done is to remove some extra characters in several columns of the data before it is exported. But since my table name changes every time, I can't just run a saved query. I thought I could write the SQL to run before exporting in the OnClick Event of btn_Export_Click, and account for the changing value of the textbox tb_Name. However, I'm unable to figure out how to draw tb_Name into the Update Statement successfully.

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. If IsNull(Me.tb_Name) Then
  4.     MsgBox "Please re-enter the table name you wish to export to text."
  5.     Else:
  6.     strSQL = "UPDATE Me.tb_Name SET [SVC_FRM_DT] = Left([SVC_FRM_DT],Len([SVC_FRM_DT])-11)," & _
  7.         "[SVC_TO_DT] = Left([SVC_TO_DT],Len([SVC_TO_DT])-11)," & _
  8.         "[CKPY_PAY_DT] = Left([CKPY_PAY_DT],Len([CKPY_PAY_DT])-11)," & _
  9.         "[BLBL_FNDG_FROM_DT] = Left([BLBL_FNDG_FROM_DT],Len([BLBL_FNDG_FROM_DT])-11)," & _
  10.         "[BLBL_FNDG_THRU_DT] = Left([BLBL_FNDG_THRU_DT],Len([BLBL_FNDG_THRU_DT])-11)," & _
  11.         "[BLIV_CREATE_DTM] = Left([BLIV_CREATE_DTM],Len([BLIV_CREATE_DTM])-12);"
  12.     CurrentDb.Execute strSQL, dbFailOnError
  13.  
  14.     DoCmd.TransferText acExportFixed, "Export_Text_Files", Me.tb_Name, "I:\Regence UMP claims\Mar 2011\Regence Claims_" & Me.tb_Name & ".txt", False
  15.     MsgBox "Export Complete! The file is here: " & "I:\Regence UMP claims\Mar 2011\Regence Claims_" & Me.tb_Name & ".txt"
  16. End If
Hopefully I'm not far off. Any help would be appreciated. Thanks!
May 4 '11 #1

✓ answered by bre1603

So I solved this problem on my own.

I haven't had much luck with my questions lately - are my issues too boring for the masses? :/

Anyway, I was able to reference the control as the table in the UPDATE statement. It was a matter of formating that reference to make sense to Access as to what I wanted.

Line 6 from the above code now reads:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE [" & Me.tb_Name & "] SET [SVC_FRM_DT] = Left([SVC_FRM_DT],Len([SVC_FRM_DT])-11)," & _
The rest of my code works fine and is unchanged.

Although I didn't get an external answer to my question, sometimes it just helps to talk out loud. Thanks for listening. :)

2 3181
bre1603
39
So I solved this problem on my own.

I haven't had much luck with my questions lately - are my issues too boring for the masses? :/

Anyway, I was able to reference the control as the table in the UPDATE statement. It was a matter of formating that reference to make sense to Access as to what I wanted.

Line 6 from the above code now reads:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE [" & Me.tb_Name & "] SET [SVC_FRM_DT] = Left([SVC_FRM_DT],Len([SVC_FRM_DT])-11)," & _
The rest of my code works fine and is unchanged.

Although I didn't get an external answer to my question, sometimes it just helps to talk out loud. Thanks for listening. :)
May 4 '11 #2
NeoPa
32,556 Expert Mod 16PB
It looks like you've already discovered the problem here. Me is a VBA reference specific to the module the code runs in. If that is included in a string and passed to the SQL engine then it is neither VBA nor the local module.

In some circumstances a fuller reference to a control on your form is possible. In this case though, the SQL syntax expects a literal string to represent the name of the table. Your solution is the most appropriate way to handle this type of scenario.
May 10 '11 #3

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

Similar topics

1
by: Bryan | last post by:
Hi, I have two tables. I want to update two columns in my first table, and with two values and held in my #temp table but based on a RUN_DATE from my first table. Can anyone point me in...
2
by: Paul .V. | last post by:
I have a report which I call to print from a form. For security reasons I have created a global variable: Const Company = "This Test Company" I call that constant whereever I want the company...
2
by: SueA | last post by:
Can anyone help me with the code to retrieve the name of the table that a control on a form (that's built off a query) refers to? For instance: qryMfrsAndBrands is based on the tables:...
5
by: laurentc | last post by:
Dear all, I have several tables based on exactly the same fields (Key/Date/Price1/Price2). I do some statistics on the prices. However, as I have many different tables (the tables are...
28
by: jverri01 | last post by:
First, I am relatively new to working with variables. Most of my experience has been with interface design. i am using ACCESS ver. 2003, running in Windows XP. Second, I spent an hour searching...
1
by: mrobinsc | last post by:
** This SQL statement returns 4 rows SELECT COUNT(*) G.ACTIVITY_ID G.RESOURCE_TYPE G.RESOURCE_CATEGORY G.RESOURCE_SUB_CAT G.ANALYSIS_TYPE G.PROJECT_ID
6
by: simon.robin.jackson | last post by:
Ok. I need to develop a macro/vba code to do the following. There are at least 300 corrections and its expected for this to happen a lot more in the future. Therefore id like a nice...
4
by: theJonster | last post by:
Is there anyway that a query could update a table name and all the objects that reference it. e.g table name is 'Blah' change to 'BlahBLahBLah' and this will update all the objects in the...
4
by: userdave | last post by:
Hi, Please can you assist me, I'm trying to execute some SQL to update 1 column to a constant value (50) in multiple rows in Table A based on an associated 'SOURCE' value in table B Table A...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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,...

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.