Another Access Scripting Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am discovering that sometimes these projects are like similar to tackling a
plumbing project at home, you start, go to loews to get what you need,
proceed, go back to loews to get what you need, etc etc.

Can someone tell me the difference between the SQL Access builds its queries
with and the SQL required by a VBScript to function? So far I have learned
the very basics of SQL, but have been using the query builder and then
switching to SQL view to cut and paste and get my union queries, etc.

I am trying to do the same thing with a script but it just send me in
circles calling for "expected end of statement", "expected ")"".

I hate posting a request for help on a new issue after every resolution you
guys give me. Thanks
-
Jeff C
Live Well .. Be Happy In All You Do
 
Jeff C said:
I am discovering that sometimes these projects are like similar to tackling
a
plumbing project at home, you start, go to loews to get what you need,
proceed, go back to loews to get what you need, etc etc.

Can someone tell me the difference between the SQL Access builds its
queries
with and the SQL required by a VBScript to function? So far I have
learned
the very basics of SQL, but have been using the query builder and then
switching to SQL view to cut and paste and get my union queries, etc.

I am trying to do the same thing with a script but it just send me in
circles calling for "expected end of statement", "expected ")"".

Hi Jeff ...

What's the offending code?

Keith.
www.keithwilby.com
 
Hi Jeff,

The SQL is basically the same. One key difference is the functions available
for use in calculated fields and other expressions. If you're working in
Access or automating an Access.Application object and using (e.g.)
DoCmd.OpenQuery you can call most standard Access VBA functions in the
query, and also your own VBA functions, e.g.

SELECT Field1, MyCustomFunction([Field2], [Field3]) As MyField FROM
tblT;

If you're using the DAO library instead, you can only use functions that are
built into Jet SQL.

Similarly, if you're using Access and a form is open, you can use the values
of controls on the form as parameters in a query with syntax like this in
the SQL of a query, e.g.

WHERE SomeField = Forms!FormName!ControlName

but in most other circumstances you have to write code that assembles a
string containing the SQL statement you want to execute.

There are some special quirks when it comes to automating Access (this is
one reason I prefer to use DAO for scripting). See
http://support.microsoft.com/kb/210111/ for more.
 
Thanks for your reply John:

I have been looking at the material you recommended yesterday too, honestly
you are really REALLY far ahead of me on most of the material but I am
working on it. I could not for the life of me figure out how to use your
DAOExecute.VBS script.

The problem SQL I am having is with an update query which is removing spaces
and another update query that inserts a colon. I have cut and pasted the SQL
from Access and manipulated it a number of times.

The database I built with all the queries works, basically importing data
and then cleaning it and moving it reformatting text into proper date and
time fields. Since it is based on reports coming from outside access and I
was able to automate the import/append of the raw data, I thought I would
just try and automate the action queries too.

The following is the .VBS file I have and it works with the exception of the
second query which is set apart with a space before and after.

const acimportFixed = 1
const acQuitSaveNone = 2
set app = createobject("Access.Application")
app.OpenCurrentDatabase("U:\jeff.mdb")
app.DoCmd.TransferText 1, "2Import","tbl_1","S:\Text Files\1.txt"
app.DoCmd.TransferText 1, "2Import","tbl_2","S:\Text Files\2.txt"
app.DoCmd.TransferText 1, "3Import","tbl_3","S:\Text Files\3.txt"
app.DoCmd.TransferText 1, "4Import","tbl_4","S:\Text Files\4.txt"
app.DoCmd.DeleteObject acTable, "1_ImportErrors"
app.DoCmd.DeleteObject acTable, "2_ImportErrors"
app.DoCmd.DeleteObject acTable, "3_ImportErrors"
app.DoCmd.DeleteObject acTable, "4_ImportErrors"
app.DoCmd.RunSQL "DELETE tbl_1.TResult FROM tbl_1 WHERE (((tbl_1.TResult) Is
Null))"

app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

MsgBox "Data Import Sequence Complete.", , "Calculator"
app.quit 2
set app = nothing


TDate is a date field, TTime is a number field, all the others are text
fields.


You can see I have four tables, each table has 6 update queries that are run
which I have sequenced in VB behind a command button.

I just thought this might be a good exercise to try and figure out how to
script by running all 20 some queries in a VBScript.
 
Hi Jeff,

The commas and quotes in this look badly screwed up:
app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

It's much more readable if you use the VB line continuation symbol
(space+underscore) to break it up:

app.DoCmd.RunSQL("UPDATE tbl_1 SET " _
& "Acct = Replace([Acct], ' ', ''), " _
& "[Name] = Replace([Name], ' ', ''), " _
...
& "Phys = Replace([Phys], ' ', '');"

One possible problem is that the Replace() function is not available in
queries in older versions of Access.

A wrinkle I often use when writing anything but the simplest VBScript is
to do it in a module (with Option Explicit at the beginning!)in an
Office application other than the one I'm planning to automate with the
script. So for this one I might use Excel. I'd start by setting a
reference to the library/ies I needed (in this case Access.Application
and perhaps Microsoft Scripting Runtime, which contains the core
VBScript objects) and then declare the variables with explicit types
(which you can't do in VBScript). E.g.

Dim App As Access.Application

Doing it this way means I take advantage of the shared syntax and
functions, and get the benefit of Intellisense as I write and edit the
code. The VBA compiler warns me of many syntax errors. And it's easy to
set breakpoints and step through troublesome code.

Once the code is running happily in this form I change all the variable
declarations to be untyped - leaving the original type declaration as a
reminder

Dim App 'As Access.Application

and test the code again. Next I remove the references to the libraries:
this will reveal problems with undeclared constants such as
acimportFixed. When the code works in that form, I paste it into a new
text file and try running that as a VBScript.



Here's something simpler than DAO_Execute.vbs:

'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access.

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.
'Works as is with a CSV file. For other delimited files
'or fixed-width files, the file specification must be supplied
'in a schema.ini file in the same folder.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"

'Textfile is C:\Temp\B1.txt
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close
 
Well John: The SQL there was cut and pasted out of the access query as it
works (ver 2003).

I am printing up your response to study tonight and will post back if a
question.

I know these responses you and others make take time and effort and I
appreciate the trouble.

Thank you
--
Jeff C
Live Well .. Be Happy In All You Do


John Nurick said:
Hi Jeff,

The commas and quotes in this look badly screwed up:
app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

It's much more readable if you use the VB line continuation symbol
(space+underscore) to break it up:

app.DoCmd.RunSQL("UPDATE tbl_1 SET " _
& "Acct = Replace([Acct], ' ', ''), " _
& "[Name] = Replace([Name], ' ', ''), " _
...
& "Phys = Replace([Phys], ' ', '');"

One possible problem is that the Replace() function is not available in
queries in older versions of Access.

A wrinkle I often use when writing anything but the simplest VBScript is
to do it in a module (with Option Explicit at the beginning!)in an
Office application other than the one I'm planning to automate with the
script. So for this one I might use Excel. I'd start by setting a
reference to the library/ies I needed (in this case Access.Application
and perhaps Microsoft Scripting Runtime, which contains the core
VBScript objects) and then declare the variables with explicit types
(which you can't do in VBScript). E.g.

Dim App As Access.Application

Doing it this way means I take advantage of the shared syntax and
functions, and get the benefit of Intellisense as I write and edit the
code. The VBA compiler warns me of many syntax errors. And it's easy to
set breakpoints and step through troublesome code.

Once the code is running happily in this form I change all the variable
declarations to be untyped - leaving the original type declaration as a
reminder

Dim App 'As Access.Application

and test the code again. Next I remove the references to the libraries:
this will reveal problems with undeclared constants such as
acimportFixed. When the code works in that form, I paste it into a new
text file and try running that as a VBScript.



Here's something simpler than DAO_Execute.vbs:

'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access.

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.
'Works as is with a CSV file. For other delimited files
'or fixed-width files, the file specification must be supplied
'in a schema.ini file in the same folder.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"

'Textfile is C:\Temp\B1.txt
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close




Thanks for your reply John:

I have been looking at the material you recommended yesterday too, honestly
you are really REALLY far ahead of me on most of the material but I am
working on it. I could not for the life of me figure out how to use your
DAOExecute.VBS script.

The problem SQL I am having is with an update query which is removing spaces
and another update query that inserts a colon. I have cut and pasted the SQL
from Access and manipulated it a number of times.

The database I built with all the queries works, basically importing data
and then cleaning it and moving it reformatting text into proper date and
time fields. Since it is based on reports coming from outside access and I
was able to automate the import/append of the raw data, I thought I would
just try and automate the action queries too.

The following is the .VBS file I have and it works with the exception of the
second query which is set apart with a space before and after.

const acimportFixed = 1
const acQuitSaveNone = 2
set app = createobject("Access.Application")
app.OpenCurrentDatabase("U:\jeff.mdb")
app.DoCmd.TransferText 1, "2Import","tbl_1","S:\Text Files\1.txt"
app.DoCmd.TransferText 1, "2Import","tbl_2","S:\Text Files\2.txt"
app.DoCmd.TransferText 1, "3Import","tbl_3","S:\Text Files\3.txt"
app.DoCmd.TransferText 1, "4Import","tbl_4","S:\Text Files\4.txt"
app.DoCmd.DeleteObject acTable, "1_ImportErrors"
app.DoCmd.DeleteObject acTable, "2_ImportErrors"
app.DoCmd.DeleteObject acTable, "3_ImportErrors"
app.DoCmd.DeleteObject acTable, "4_ImportErrors"
app.DoCmd.RunSQL "DELETE tbl_1.TResult FROM tbl_1 WHERE (((tbl_1.TResult) Is
Null))"


MsgBox "Data Import Sequence Complete.", , "Calculator"
app.quit 2
set app = nothing


TDate is a date field, TTime is a number field, all the others are text
fields.


You can see I have four tables, each table has 6 update queries that are run
which I have sequenced in VB behind a command button.

I just thought this might be a good exercise to try and figure out how to
script by running all 20 some queries in a VBScript.
 
Well John: The SQL there was cut and pasted out of the access query as it
works (ver 2003).

I am printing up your response to study tonight and will post back if a
question.

I know these responses you and others make take time and effort and I
appreciate the trouble.

Thank you

--
Jeff C
Live Well .. Be Happy In All You Do


John Nurick said:
Hi Jeff,

The commas and quotes in this look badly screwed up:
app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

It's much more readable if you use the VB line continuation symbol
(space+underscore) to break it up:

app.DoCmd.RunSQL("UPDATE tbl_1 SET " _
& "Acct = Replace([Acct], ' ', ''), " _
& "[Name] = Replace([Name], ' ', ''), " _
...
& "Phys = Replace([Phys], ' ', '');"

One possible problem is that the Replace() function is not available in
queries in older versions of Access.

A wrinkle I often use when writing anything but the simplest VBScript is
to do it in a module (with Option Explicit at the beginning!)in an
Office application other than the one I'm planning to automate with the
script. So for this one I might use Excel. I'd start by setting a
reference to the library/ies I needed (in this case Access.Application
and perhaps Microsoft Scripting Runtime, which contains the core
VBScript objects) and then declare the variables with explicit types
(which you can't do in VBScript). E.g.

Dim App As Access.Application

Doing it this way means I take advantage of the shared syntax and
functions, and get the benefit of Intellisense as I write and edit the
code. The VBA compiler warns me of many syntax errors. And it's easy to
set breakpoints and step through troublesome code.

Once the code is running happily in this form I change all the variable
declarations to be untyped - leaving the original type declaration as a
reminder

Dim App 'As Access.Application

and test the code again. Next I remove the references to the libraries:
this will reveal problems with undeclared constants such as
acimportFixed. When the code works in that form, I paste it into a new
text file and try running that as a VBScript.



Here's something simpler than DAO_Execute.vbs:

'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access.

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.
'Works as is with a CSV file. For other delimited files
'or fixed-width files, the file specification must be supplied
'in a schema.ini file in the same folder.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"

'Textfile is C:\Temp\B1.txt
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close




Thanks for your reply John:

I have been looking at the material you recommended yesterday too, honestly
you are really REALLY far ahead of me on most of the material but I am
working on it. I could not for the life of me figure out how to use your
DAOExecute.VBS script.

The problem SQL I am having is with an update query which is removing spaces
and another update query that inserts a colon. I have cut and pasted the SQL
from Access and manipulated it a number of times.

The database I built with all the queries works, basically importing data
and then cleaning it and moving it reformatting text into proper date and
time fields. Since it is based on reports coming from outside access and I
was able to automate the import/append of the raw data, I thought I would
just try and automate the action queries too.

The following is the .VBS file I have and it works with the exception of the
second query which is set apart with a space before and after.

const acimportFixed = 1
const acQuitSaveNone = 2
set app = createobject("Access.Application")
app.OpenCurrentDatabase("U:\jeff.mdb")
app.DoCmd.TransferText 1, "2Import","tbl_1","S:\Text Files\1.txt"
app.DoCmd.TransferText 1, "2Import","tbl_2","S:\Text Files\2.txt"
app.DoCmd.TransferText 1, "3Import","tbl_3","S:\Text Files\3.txt"
app.DoCmd.TransferText 1, "4Import","tbl_4","S:\Text Files\4.txt"
app.DoCmd.DeleteObject acTable, "1_ImportErrors"
app.DoCmd.DeleteObject acTable, "2_ImportErrors"
app.DoCmd.DeleteObject acTable, "3_ImportErrors"
app.DoCmd.DeleteObject acTable, "4_ImportErrors"
app.DoCmd.RunSQL "DELETE tbl_1.TResult FROM tbl_1 WHERE (((tbl_1.TResult) Is
Null))"


MsgBox "Data Import Sequence Complete.", , "Calculator"
app.quit 2
set app = nothing


TDate is a date field, TTime is a number field, all the others are text
fields.


You can see I have four tables, each table has 6 update queries that are run
which I have sequenced in VB behind a command button.

I just thought this might be a good exercise to try and figure out how to
script by running all 20 some queries in a VBScript.
 
Well John: The SQL there was cut and pasted out of the access query as it
works (ver 2003).

I am printing up your response to study tonight and will post back if a
question.

I know these responses you and others make take time and effort and I
appreciate the trouble.

Thank you

--
Jeff C
Live Well .. Be Happy In All You Do


John Nurick said:
Hi Jeff,

The commas and quotes in this look badly screwed up:
app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

It's much more readable if you use the VB line continuation symbol
(space+underscore) to break it up:

app.DoCmd.RunSQL("UPDATE tbl_1 SET " _
& "Acct = Replace([Acct], ' ', ''), " _
& "[Name] = Replace([Name], ' ', ''), " _
...
& "Phys = Replace([Phys], ' ', '');"

One possible problem is that the Replace() function is not available in
queries in older versions of Access.

A wrinkle I often use when writing anything but the simplest VBScript is
to do it in a module (with Option Explicit at the beginning!)in an
Office application other than the one I'm planning to automate with the
script. So for this one I might use Excel. I'd start by setting a
reference to the library/ies I needed (in this case Access.Application
and perhaps Microsoft Scripting Runtime, which contains the core
VBScript objects) and then declare the variables with explicit types
(which you can't do in VBScript). E.g.

Dim App As Access.Application

Doing it this way means I take advantage of the shared syntax and
functions, and get the benefit of Intellisense as I write and edit the
code. The VBA compiler warns me of many syntax errors. And it's easy to
set breakpoints and step through troublesome code.

Once the code is running happily in this form I change all the variable
declarations to be untyped - leaving the original type declaration as a
reminder

Dim App 'As Access.Application

and test the code again. Next I remove the references to the libraries:
this will reveal problems with undeclared constants such as
acimportFixed. When the code works in that form, I paste it into a new
text file and try running that as a VBScript.



Here's something simpler than DAO_Execute.vbs:

'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access.

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.
'Works as is with a CSV file. For other delimited files
'or fixed-width files, the file specification must be supplied
'in a schema.ini file in the same folder.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"

'Textfile is C:\Temp\B1.txt
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close




Thanks for your reply John:

I have been looking at the material you recommended yesterday too, honestly
you are really REALLY far ahead of me on most of the material but I am
working on it. I could not for the life of me figure out how to use your
DAOExecute.VBS script.

The problem SQL I am having is with an update query which is removing spaces
and another update query that inserts a colon. I have cut and pasted the SQL
from Access and manipulated it a number of times.

The database I built with all the queries works, basically importing data
and then cleaning it and moving it reformatting text into proper date and
time fields. Since it is based on reports coming from outside access and I
was able to automate the import/append of the raw data, I thought I would
just try and automate the action queries too.

The following is the .VBS file I have and it works with the exception of the
second query which is set apart with a space before and after.

const acimportFixed = 1
const acQuitSaveNone = 2
set app = createobject("Access.Application")
app.OpenCurrentDatabase("U:\jeff.mdb")
app.DoCmd.TransferText 1, "2Import","tbl_1","S:\Text Files\1.txt"
app.DoCmd.TransferText 1, "2Import","tbl_2","S:\Text Files\2.txt"
app.DoCmd.TransferText 1, "3Import","tbl_3","S:\Text Files\3.txt"
app.DoCmd.TransferText 1, "4Import","tbl_4","S:\Text Files\4.txt"
app.DoCmd.DeleteObject acTable, "1_ImportErrors"
app.DoCmd.DeleteObject acTable, "2_ImportErrors"
app.DoCmd.DeleteObject acTable, "3_ImportErrors"
app.DoCmd.DeleteObject acTable, "4_ImportErrors"
app.DoCmd.RunSQL "DELETE tbl_1.TResult FROM tbl_1 WHERE (((tbl_1.TResult) Is
Null))"


MsgBox "Data Import Sequence Complete.", , "Calculator"
app.quit 2
set app = nothing


TDate is a date field, TTime is a number field, all the others are text
fields.


You can see I have four tables, each table has 6 update queries that are run
which I have sequenced in VB behind a command button.

I just thought this might be a good exercise to try and figure out how to
script by running all 20 some queries in a VBScript.
 
Well John: The SQL there was cut and pasted out of the access query as it
works (ver 2003).

I am printing up your response to study tonight and will post back if a
question.

I know these responses you and others make take time and effort and I
appreciate the trouble.

Thank you

--
Jeff C
Live Well .. Be Happy In All You Do


John Nurick said:
Hi Jeff,

The commas and quotes in this look badly screwed up:
app.DoCmd.RunSQL(UPDATE )'tbl_1 SET tbl_1.Acct = Replace([Acct],’ ‘,’’),
tbl_1.Name = Replace([Name],’ ‘,’’), tbl_1.TDate = Replace([TDate],’ ‘,’’),
tbl_1.TResult = Replace([TResult],’ ‘,’’), tbl_1.TTime = Replace([TTime],’
‘,’’), tbl_1.Phys = Replace([Phys],’ ‘,’’))

It's much more readable if you use the VB line continuation symbol
(space+underscore) to break it up:

app.DoCmd.RunSQL("UPDATE tbl_1 SET " _
& "Acct = Replace([Acct], ' ', ''), " _
& "[Name] = Replace([Name], ' ', ''), " _
...
& "Phys = Replace([Phys], ' ', '');"

One possible problem is that the Replace() function is not available in
queries in older versions of Access.

A wrinkle I often use when writing anything but the simplest VBScript is
to do it in a module (with Option Explicit at the beginning!)in an
Office application other than the one I'm planning to automate with the
script. So for this one I might use Excel. I'd start by setting a
reference to the library/ies I needed (in this case Access.Application
and perhaps Microsoft Scripting Runtime, which contains the core
VBScript objects) and then declare the variables with explicit types
(which you can't do in VBScript). E.g.

Dim App As Access.Application

Doing it this way means I take advantage of the shared syntax and
functions, and get the benefit of Intellisense as I write and edit the
code. The VBA compiler warns me of many syntax errors. And it's easy to
set breakpoints and step through troublesome code.

Once the code is running happily in this form I change all the variable
declarations to be untyped - leaving the original type declaration as a
reminder

Dim App 'As Access.Application

and test the code again. Next I remove the references to the libraries:
this will reveal problems with undeclared constants such as
acimportFixed. When the code works in that form, I paste it into a new
text file and try running that as a VBScript.



Here's something simpler than DAO_Execute.vbs:

'Sample VBScript to import data from a textfile into
'a table in an MDB database without opening Access.

'Modify DB_NAME, TBL_NAME, DATA_SOURCE as required
'and the code that builds strSQL as necessary.

'If TBL_NAME exists, appends to it; otherwise creates it.
'Works as is with a CSV file. For other delimited files
'or fixed-width files, the file specification must be supplied
'in a schema.ini file in the same folder.

Option Explicit

Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String

Const DB_NAME = "C:\Temp\Test 2003.mdb"
Const TBL_NAME = "My_Table"

'Textfile is C:\Temp\B1.txt
Const DATA_SOURCE = "[Text;HDR=Yes;Database=C:\Temp\;].B1#txt"

Set oJet = CreateObject("DAO.DBEngine.36")
Set oDB = oJet.OpenDatabase(DB_NAME)

For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next

If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If

oDB.Execute strSQL

oDB.Close




Thanks for your reply John:

I have been looking at the material you recommended yesterday too, honestly
you are really REALLY far ahead of me on most of the material but I am
working on it. I could not for the life of me figure out how to use your
DAOExecute.VBS script.

The problem SQL I am having is with an update query which is removing spaces
and another update query that inserts a colon. I have cut and pasted the SQL
from Access and manipulated it a number of times.

The database I built with all the queries works, basically importing data
and then cleaning it and moving it reformatting text into proper date and
time fields. Since it is based on reports coming from outside access and I
was able to automate the import/append of the raw data, I thought I would
just try and automate the action queries too.

The following is the .VBS file I have and it works with the exception of the
second query which is set apart with a space before and after.

const acimportFixed = 1
const acQuitSaveNone = 2
set app = createobject("Access.Application")
app.OpenCurrentDatabase("U:\jeff.mdb")
app.DoCmd.TransferText 1, "2Import","tbl_1","S:\Text Files\1.txt"
app.DoCmd.TransferText 1, "2Import","tbl_2","S:\Text Files\2.txt"
app.DoCmd.TransferText 1, "3Import","tbl_3","S:\Text Files\3.txt"
app.DoCmd.TransferText 1, "4Import","tbl_4","S:\Text Files\4.txt"
app.DoCmd.DeleteObject acTable, "1_ImportErrors"
app.DoCmd.DeleteObject acTable, "2_ImportErrors"
app.DoCmd.DeleteObject acTable, "3_ImportErrors"
app.DoCmd.DeleteObject acTable, "4_ImportErrors"
app.DoCmd.RunSQL "DELETE tbl_1.TResult FROM tbl_1 WHERE (((tbl_1.TResult) Is
Null))"


MsgBox "Data Import Sequence Complete.", , "Calculator"
app.quit 2
set app = nothing


TDate is a date field, TTime is a number field, all the others are text
fields.


You can see I have four tables, each table has 6 update queries that are run
which I have sequenced in VB behind a command button.

I just thought this might be a good exercise to try and figure out how to
script by running all 20 some queries in a VBScript.
 
Sorry about all the replies yesterday, kept getting error message
butevidently "that" was an error.

I decided to first try your script.

I changed DB_NAME to the full path to a database I created.
I changed TBL_Name to the name of a table I defined in that database
I changed DATA_SOURCE to the full path of the text file which is the source
of data

The text file is fixed length so I built a Schema.ini file.

Your script, the database and the Schema file all reside in the same folder.

I get an error when it runs "cannot find file 'full path to the text file'.
The name of the file it cannot find is the name of the text file with the
extension .mdb.
 
Among the possibilities:

-Maybe you did't get the syntax of DATA_SOURCE quite right. You have to
separate the path, the filename and the extension.

-You say you had the script, the MDB and schema.ini in the same folder.
YOu don't mention the text file: schema.ini must be in the same folder
as the text file, not the script or database.

-Is it certain that the schema.ini entry for the text file is correct?
Did you test it by using TransferText to import the text file?

If it's none of those, do what I'd do: paste the script into an Excel or
VB module and try stepping through it.
 
Back
Top