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 March 10th, 2005, 03:52 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to generate sql insert using generateInserts()

Hi guys. I want to know how i can generate SQL insert statements for my database called Northwind and save it in text file using VBA routine Generateinserts().
I be happy if some one show me how .Thanks
 
Old March 10th, 2005, 08:08 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

What exactly qare you trying to accomplish?

Looking at your post (and trying to discern what you mean), I think there is probably a better way to do what you are after.

Please try to use your shift key at the appropriate points; that makes for [u]MUCH</u> easier reading. (“I” is capital, “SQL” is capital, “VBA” is capital and function names like “generateinserts()” are clearer when they are more like “GenerateInserts().”)
A period after “Hi guys” would be helpful too.
 
Old March 11th, 2005, 10:19 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

It sounds to me like your goal is to populate a database with data.

What you seem to be asking is to have a text file that is essentially a series of insert statements, one for each record.

I'm guessing. But I think that once Brian gets beyond being annoyed at your typing skills, he'll tell you to store the data in a text file or Excel file, link to that file using a Linked table in Access and just build an Append query from the linked table to your database table. Of course you'll need to do this for each table. And you'll have to populate related tables in the right order.

I'm sure somebody out there will say this is an ideal situation for a macro. (Is that mmcdonal that would say that?) At any rate, once you get your files linked as tables and your append queries built, just create a macro that will run the queries in the right order.

I can understand why you desire a text file to have all the Insert Into statements for each record. But that will take a lot longer to build than the technique mentioned above.

But if that's what you want to do, it's not that difficult. You'll simply write a routine that opens a text file for output, then opens a record set on your table. Step through each record in the record set and write a line in the text file with the Insert statement. Something like,

INSERT INTO Table1 ( Field1, Field2 ) SELECT "Test" AS Expr1, "Test" AS Expr2 FROM Table1;

You can use some techniques to more easily build the insert statement. Like:

' This builds the list of fields to insert into
For each field in recordset
   strIntoList = strIntoList & field.name & ", "
next field

' This builds the values for the select
For each field in recordset
   strSelectValues = strSelectValues & recordset(field.name).value & ", "
next field

You'll have to do things like add quotes (") for text strings and hash marks (#) for dates. Sorry, I'm not going to write all of the code for you. I generally get paid to do that. But that should start you on the right track.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old March 11th, 2005, 12:03 PM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by BrianWren
 What exactly qare you trying to accomplish?

Looking at your post (and trying to discern what you mean), I think there is probably a better way to do what you are after.

Please try to use your shift key at the appropriate points; that makes for [u]MUCH</u> easier reading. (“I” is capital, “SQL” is capital, “VBA” is capital and function names like “generateinserts()” are clearer when they are more like “GenerateInserts().”)
A period after “Hi guys” would be helpful too.
Thank u for your nice reply!
i try to do some programing with meta data using object models. There might be better ways to do it but my intention is to do some meta data programing!

 
Old March 12th, 2005, 12:17 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey, I wasn't knocking Brian. We've all had days where we lash out at the wrong people for the wrong reason. I'm sure he wasn't picking on you specifically. It was just one of those days. I'm sure one day he'll have the perfect answer for one of your questions. No hard feelings.

I thought about your "project" some more. The procedure you're thinking about writing could easily be set up to take parameters allowing you to write your insert statements for any table to any file. Just have your parameters be the table name and the file name. For that matter, you could construct your file name from the table name. Then you'd have a consistent naming convention.

I'd definitely recommend using the File System Object for the file output.

BTW, Brian. Since you commented about writing techniques... I initially read most posts from this site in my email. Because of all of the formating you use, I have difficulty reading your posts. The formating just doesn't come through in my email. Instead I get brackets and slashes and the like. In fact, I usually end up opening the site to read your posts. But I'm not complaining. Just noting that to you in case it makes a difference to you.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot generate insert statements in datasource Maxxim ASP.NET 2.0 Professional 1 July 6th, 2007 08:04 AM
how generate ThePhileII-Load.sql once chinahyf BOOK: ASP.NET Website Programming Problem-Design-Solution 2 December 25th, 2005 09:18 PM
Generate SQL Script shaileshk SQL Server 2000 3 September 6th, 2005 03:12 PM
How to generate SQL querry ? mistry_bhavin ADO.NET 1 August 12th, 2004 01:15 PM
How to generate SQL querries mistry_bhavin General .NET 1 August 12th, 2004 11:42 AM





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