By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,635 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,635 IT Pros & Developers. It's quick & easy.

VBA Code "Query input must contain at least one table or query"

P: 5
I want to take all the fields in TBLCommissionTransactions in current DB and make a new table in T:\folder\TblBackups.mdb and append today’s date and time to the new table name. This is purely for archival purposes.


Sorry, not really well versed in VBA. I have the following code which I modified from search online. When I run it I get the message "Query input must contain at least one table or query" which I am suspecting is failing on strTableName but don't really know for sure. I have played around with it and can't get it to work. What am I doing wrong?
Expand|Select|Wrap|Line Numbers
  1. Private Sub BackupTblCommissions_Click()
  2. Dim strTableName As String
  3. Dim strFilename As String
  4. Dim strSQL As String
  5. strFilename = "T:\folder\TblBackups.mdb"
  6. strTableName = "TBLCommissionTransactions" & Format(Now(), "yyyymmdd-hhmm")
  7. strSQL = "SELECT TBLCommissionTransactions.* INTO " & strTableName & " IN " & _
  8. strFilename & " FROM TBLCommissionTransactions;"
  9.  
  10. CurrentDb.Execute strSQL
  11. End Sub
Apr 4 '12 #1
Share this Question
Share on Google+
9 Replies


NeoPa
Expert Mod 15k+
P: 31,409
mleezon:
What am I doing wrong?
Everything I would say. The whole concept is ill-advised, starting with multiple tables with the same layout. Please read Database Normalisation and Table Structures as it's a very important concept that you really need to appreciate before designing anything at all in a database.

I can't imagine this particular question will be any use to you once you have a basic understanding of Normalisation under your belt, but you might want to post something else (in a new thread) at that point.
Apr 5 '12 #2

P: 5
I think your reply does not apply to the original question.
Apr 5 '12 #3

P: 1
mleezon,

You have to create the table first(Using VBA in the same function) and then append the data to it.

Code to create table:
Expand|Select|Wrap|Line Numbers
  1.    Dim dbsdb1 As Database
  2.    Dim tdfNew As TableDef
  3.    Dim prpLoop As Property
  4.    TblName = "TBLCommissionTransactions" & Format(Now(), "yyyymmdd-hhmm") 
  5.  
  6.    Set dbsdb1 = OpenDatabase("T:\folder\TblBackups.mdb")
  7.  
  8.    ' Create a new TableDef object.
  9.    Set tdfNew = dbsdb1.CreateTableDef(TblName)
  10.  
  11.    With tdfNew
  12.       .Fields.Append .CreateField("F1", dbText)
  13.       .Fields.Append .CreateField("F2", dbText)
  14.       .Fields.Append .CreateField("F3", dbText)
  15.       .Fields.Append .CreateField("F4", dbText)
  16.       .Fields.Append .CreateField("F5", dbText)
  17.  
  18.       dbsdb1.TableDefs.Append tdfNew
  19.  
  20.    End With
*****
Here you use the append/Insert SQL command.
****
Expand|Select|Wrap|Line Numbers
  1.    dbsdb1.Close
Apr 5 '12 #4

NeoPa
Expert Mod 15k+
P: 31,409
mleezon:
I think your reply does not apply to the original question.
I can't imagine what would lead you to think that.

Your choice of course, but it's unfortunate that you can't see what would be obvious to anyone with database experience.
Apr 5 '12 #5

P: 5
I must have disturbed the database gods.

As I never discussed or asked a question about or even showed anything regarding the structure of my data tables I do not think that the page you referred me to "Database Normalization and Table Structures" applies. I am simply looking for a way to back up certain tables within a database from VBA. And yes, I am self taught with databases and am always willing to learn when someone is willing to teach, but smug responses to make someone feel superior do not teach anything.
Apr 6 '12 #6

P: 5
Here is corrected code that works:

Expand|Select|Wrap|Line Numbers
  1. strFilename = "'T:\folder\TblBackups.mdb'"
  2. strTableName = "[TBLCommissionTransactions" & Format(Now(), "yyyymmdd-hhmm" & "]")
  3. strSQL = "SELECT TBLCommissionTransactions.* INTO " & strTableName & " IN " & _
  4. strFilename & " FROM TBLCommissionTransactions;"
  5.  
Thanks to Remou
Apr 6 '12 #7

NeoPa
Expert Mod 15k+
P: 31,409
mleezon:
As I never discussed or asked a question about or even showed anything regarding the structure of my data tables I do not think that the page you referred me to "Database Normalization and Table Structures" applies. I am simply looking for a way to back up certain tables within a database from VBA.
I'm sorry you feel that way. Actually, the very fact that you are considering making multiple copies of a table (ignoring that they might hold different data as that is irrelevant to database design) is indication enough to most people (at least with some database design experience) that you don't have a good understanding of the concepts of Normalisation. There's nothing magic or superior about it. I'm sure you would be able to pick up on the same sort of stuff in subjects that you know something about.
mleezon:
I am self taught with databases and am always willing to learn when someone is willing to teach, but smug responses to make someone feel superior do not teach anything.
I would suggest this is not actually true. I would certainly hope that you could learn something from this, but your attitude seems to be so much on the defensive, that you can't even consider that I may be right. If you can get over that, you might find you'll save yourself some hassle. On the other hand, if you continue with your intended approach, and also continue to work in databases, you will surely reach a point where you will appreciate what I tried to save you from. If you think that anyone could feel superior or smug from stating one of the very basics of the subject though, then I would suggest that merely reflects your own confusion.

Ultimately, how you proceed is down to you of course. I'm comfortable at least that I did what I could to try to warn you.
Apr 6 '12 #8

P: 5
How about the possibility that before an import procedure that appeneds data into the table in question I would like to make a backup of a table? I don't think that making multiple copies of tables for backup purposes would apply to your normalization document. It is simply that, a back up in case I should need it if the import goes wrong. Most likley I would not, but when I do it is there backed up.
Apr 6 '12 #9

NeoPa
Expert Mod 15k+
P: 31,409
mleezon:
How about the possibility that ...
Making a backup wouldn't indicate that, but naming a table with the date would. Think about it.

PS. We don't encourage OPs to select their own posts as Best Answer. In very rare circumstances we can allow this, but your answer is simply putting some code to the concept provided for you by devu21. It explains nothing and is from the OP (The Original Poster - which is whichever member it is who asks the original question. IE. yourself in this case). As such, it doesn't qualify as a Best Answer.
Apr 8 '12 #10

Post your reply

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