Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2008, 05:31 AM
Registered User
 
Join Date: Jan 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using a form field to direct an INSERT INTO SQL

Hi there,
I have about 20 SQL statements in MS Access 2003 that appends data from one central database to 10 different end-user databases on timer commands. They look a little something like this:

DoCmd.RunSQL "INSERT INTO tblKPI3Results ( EstLabHrsReqd )SELECT [qryKPI3Results].EstLabHrsReqd IN 'K:\Data\AMT\Reporting\AMT Reporting Database.mdb' FROM [qryKPI3Results];"

My question is, can I change the target database by using the value of a field on a form? Im my mind it would have looked something like the following:

(Where Text1 = "K:\Data\AMT\Reporting\AMT Reporting Database.mdb" on the form)

Dim DBLocation as String
Set DBLocation = Me.Text1

DoCmd.RunSQL "INSERT INTO tblKPI3Results ( EstLabHrsReqd )SELECT [qryKPI3Results].EstLabHrsReqd IN BDLocation FROM [qryKPI3Results];"


Can this be done, or does the SQL string have to remain as a text string?

Any help appreciated
 
Old January 25th, 2008, 08:21 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Yes, this can bo done. Yes, the SQL string has to remain a string.

Do this:

Create a combo with a list of your target databases. Then do this with your code:

Dim sTarget As String
Dim sSQL As String

If IsNull(Me.cboTarget) or Me.cboTarget = "" Then
   MsgBox "Please select a target database", vbCritical
   Exit Sub
Else
   sTarget = Me.cboTarget
End If

sSQL = "INSERT INTO tblKPI3Results ( EstLabHrsReqd )SELECT [qryKPI3Results].EstLabHrsReqd IN '" & sTarget & "' FROM [qryKPI3Results];"

DoCmd.RunSQL sSQL

Did that help? It is not a good idea to add non-variables to a SQL string at runtime, that is why you post that cbo value to a string, then concatenate the string into your SQL string, then run your SQL string once it is built. Access has a big problem with this:

DoCmd.RunSQL "INSERT INTO tblKPI3Results ( EstLabHrsReqd )SELECT [qryKPI3Results].EstLabHrsReqd IN '" & Me.cboTarget & "' FROM [qryKPI3Results];"



mmcdonal

Look it up at: http://wrox.books24x7.com
 
Old January 25th, 2008, 09:50 AM
Registered User
 
Join Date: Jan 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mate. It worked sweet.

I owe you a beer next time you are in town.





Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP code to insert values from HTML form to SQL se rbyamukama Pro JSP 3 January 29th, 2013 07:42 AM
insert form data in an sql and email the content ddnk77872 PHP Databases 3 February 13th, 2007 09:47 AM
Chapter-13-direct SQL command-ExecuteNonQuery erro lionking BOOK: Beginning ASP.NET 1.0 3 May 13th, 2004 10:42 PM
Chapter-13-direct SQL command-ExecuteNonQuery erro softdev BOOK: Beginning ASP.NET 1.0 6 February 22nd, 2004 07:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.