Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Issue w/ AutoNumber Field during DAO Record Insert

All -

I have reviewed the sample code at
http://www.bygsoftware.com/Excel/SQL/UsingSql.html
regarding the creation of an Access Table from Excel utilizing the DAO
library.

Below is the sample Create Table SQL Stmt from their web site:

vtSql = ""
vtSql = vtSql & " CREATE TABLE " & ctSheet & " ("
''Loop around each column to create the SQL code
''Column names must not contain spaces
With ActiveCell.CurrentRegion
For viCount = 1 To viCols
vtSql = vtSql & .Cells(1, viCount) & "x " & _
fGetCellFormat(.Cells(2, viCount))
If viCount < viCols Then
vtSql = vtSql & ", "
Else
vtSql = vtSql & ")"
End If
Next
End With

dbs.Execute vtSql

(ctSheet in the above example is Access Table Name & a Worksheet Tab Name in
Excel. The fGetCellFormat function is in the module. The code picks up the
Field Names from the Worksheet).


I wanted to add an AutoNumber field to identify each record as unique. So I
research the web and found this piece of code. It worked and when added to
the above code it created the AutoNumber field I desired.


'Insert AutoNumber Field
Set t= dbs.TableDefs(ctSheet)
Set f= t.CreateField("xAutoNumberField", dbLong)
f.Attributes = f.Attributes + dbAutoIncrField
f.OrdinalPosition = 0
t.Fields.Append f


(Note: all the proper Dim Stmts are in the procedure)


---- So far so good. The Table has all the fields & the AutoNumber Field.


The next procedure I run is the Insert SQL (also from the
www.bygsoftware.com web site).


vtSql = ""
vtSql = vtSql & " INSERT INTO " & ctSheet
vtSql = vtSql & " VALUES ("
For viCount = 1 To viCols
Select Case fGetCellFormat(.Cells(2, viCount))
Case "TEXT"
vtWrapChar = """"
Case "DATETIME"
vtWrapChar = "#"
Case Else
vtWrapChar = ""
End Select

vtSql = vtSql & vtWrapChar & _
..Cells(viRcount, viCount) & vtWrapChar

If viCount < viCols Then
vtSql = vtSql & ","
Else
vtSql = vtSql & ")"
End If
Next

dbs.Execute vtSql


(The above SQL is picking up the same fields headers as created in the Table
SQL above with the exception of the AutoNumber Field).


Here is the issue:

None of the Insert Records loaded.


Questions:
1. Am I supposed to modifed the INSERT INTO SQL to account for the new
AutoNumber Field? If yes, how do I do that?

2. The f.OrdinalPosition = 0 stmt in the CREATE TABLE procedure does not
place the AutoNumber field in the 1st position in the Access Table. It
placed the AutoNumber field in 2nd position (it looks like this:
CompanyName, xAutoNumberField, SalesPersonID, etc. instead of this:
xAutoNumberField, CompanyName, SalesPersonID, etc.)


Thank you for your help.


MSweetG222

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default Issue w/ AutoNumber Field during DAO Record Insert

All -

Dicks-Blog has been working on the same topic. Belwo is the link.

The difference in "Dicks-Blog's" "Insert Into" Stmt than "bygsoftware's" is
that in Dicks-Blog SQL Stmt lists the Column Header to be loaded w/in the SQL
stmt. When I added this to my SQL code, I did not have any further issues.

Here is the link to his site:

http://www.dicks-blog.com/archives/2...tables-in-vba/


Thx
MSweetG222

=========================================

"MSweetG222" wrote:

All -

I have reviewed the sample code at
http://www.bygsoftware.com/Excel/SQL/UsingSql.html
regarding the creation of an Access Table from Excel utilizing the DAO
library.

Below is the sample Create Table SQL Stmt from their web site:

vtSql = ""
vtSql = vtSql & " CREATE TABLE " & ctSheet & " ("
''Loop around each column to create the SQL code
''Column names must not contain spaces
With ActiveCell.CurrentRegion
For viCount = 1 To viCols
vtSql = vtSql & .Cells(1, viCount) & "x " & _
fGetCellFormat(.Cells(2, viCount))
If viCount < viCols Then
vtSql = vtSql & ", "
Else
vtSql = vtSql & ")"
End If
Next
End With

dbs.Execute vtSql

(ctSheet in the above example is Access Table Name & a Worksheet Tab Name in
Excel. The fGetCellFormat function is in the module. The code picks up the
Field Names from the Worksheet).


I wanted to add an AutoNumber field to identify each record as unique. So I
research the web and found this piece of code. It worked and when added to
the above code it created the AutoNumber field I desired.


'Insert AutoNumber Field
Set t= dbs.TableDefs(ctSheet)
Set f= t.CreateField("xAutoNumberField", dbLong)
f.Attributes = f.Attributes + dbAutoIncrField
f.OrdinalPosition = 0
t.Fields.Append f


(Note: all the proper Dim Stmts are in the procedure)


---- So far so good. The Table has all the fields & the AutoNumber Field.


The next procedure I run is the Insert SQL (also from the
www.bygsoftware.com web site).


vtSql = ""
vtSql = vtSql & " INSERT INTO " & ctSheet
vtSql = vtSql & " VALUES ("
For viCount = 1 To viCols
Select Case fGetCellFormat(.Cells(2, viCount))
Case "TEXT"
vtWrapChar = """"
Case "DATETIME"
vtWrapChar = "#"
Case Else
vtWrapChar = ""
End Select

vtSql = vtSql & vtWrapChar & _
.Cells(viRcount, viCount) & vtWrapChar

If viCount < viCols Then
vtSql = vtSql & ","
Else
vtSql = vtSql & ")"
End If
Next

dbs.Execute vtSql


(The above SQL is picking up the same fields headers as created in the Table
SQL above with the exception of the AutoNumber Field).


Here is the issue:

None of the Insert Records loaded.


Questions:
1. Am I supposed to modifed the INSERT INTO SQL to account for the new
AutoNumber Field? If yes, how do I do that?

2. The f.OrdinalPosition = 0 stmt in the CREATE TABLE procedure does not
place the AutoNumber field in the 1st position in the Access Table. It
placed the AutoNumber field in 2nd position (it looks like this:
CompanyName, xAutoNumberField, SalesPersonID, etc. instead of this:
xAutoNumberField, CompanyName, SalesPersonID, etc.)


Thank you for your help.


MSweetG222

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access autonumber field changes to date format when Excel imports NDC Excel Discussion (Misc queries) 0 February 17th 10 11:05 PM
Duplicate sheet, autonumber sheet, record data on another sheet des-sa[_2_] Excel Worksheet Functions 0 May 8th 08 06:56 PM
Highlight Record and Field Gator Excel Discussion (Misc queries) 1 January 4th 08 04:44 PM
how I can sum or subtract 2 or 3 field in access in any record hadi New Users to Excel 1 November 4th 06 10:27 PM
HOW DO INSERT AN AUTONUMBER INTO AN INVOICE TEMPLATE IN EXCEL JW69 Excel Worksheet Functions 8 August 10th 06 04:01 AM


All times are GMT +1. The time now is 03:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"