VBA Append to Another DB (1 Viewer)

Graham63

Registered User.
Local time
Today, 05:26
Joined
Mar 22, 2012
Messages
20
Hi,

I am by no means an expert and all I know is what I have taught myself. I am creating a db to micro manage elements of projects I work on.

I have an unbound form which I found and adapted online. On the form there are 2 buttons and a text field. One button runs vba code and opens a dialog box and inserts the file path of any DB I select located on my drive in the form's text box. All good

The other button deletes table data and appends records to the file at the path in the text box. Like an export to a user DB?

My problem is the deletes work fine but I cant seem to get the variable file name/syntax right in the DoCmd.RunSQL statement, see a sample below, where am I going wrong? I am also using the variable elsewhere to OpenDatabase(strFileName) for the deletes so its right there. This is a sample of what I have. I get the error "could not find file path" With everything up to my folder and the variable string name as written not the full path string I'm looking for C:\Folder\strFileName in the error message.

Dim strFileName As String
strFileName = Me.TextBoxPath.Value

DoCmd.RunSQL ("INSERT INTO [Projects] IN 'strFileName'" & _
"SELECT [Projects].*" & _
"FROM [Projects]" & _
"WHERE (((Projects.ProjectID)=[Forms]![Projects Dashboard]![Combo519];)

Thanks in advance
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:26
Joined
Aug 30, 2003
Messages
36,118
Well, not how I would do it but you have to concatenate the path:

DoCmd.RunSQL ("INSERT INTO [Projects] IN '" & strFileName & "'" & _

Also watch out for spacing between lines ("FROM [Projects]WHERE").
 

Graham63

Registered User.
Local time
Today, 05:26
Joined
Mar 22, 2012
Messages
20
I Know I Know, its a hobby with me and I have started to put some of it into practice. I've read a few things on this one and I think the best route would probably have been to link the tables but I don't think the user will be able to use my network so I would be emailing the BE.

Oh, obviously this worked thanks very much. I would be interested in how you would achieve it differently if you have a moment to send me your thoughts.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:26
Joined
Aug 30, 2003
Messages
36,118
Oh, I wasn't saying you shouldn't do it that way or anything, just that the "IN" method wasn't something I normally use. That's as much because I use SQL Server more than Access for the back end.

My first option would be linking the tables, perhaps providing the user a way to point to the db and then linking to it. My second thought was a db variable that pointed to the external database, but that wouldn't work with a mixed situation like yours (source and destination in different databases). Failing linked tables, your way is fine.
 

Users who are viewing this thread

Top Bottom