Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Excel Templates and databases

I have a VBA / Excel application that does the the following:
in the Workbook_Open event, I query a MS Access database to
populate a hidden worksheet. (Have a number of named ranges
on my user's visible worksheet that are dependent on values that
I populate from the hidden.)

During the Worksheet_Change event, I have a function that queries
the same database file for lookup information once the user selects
some criterion.

I'd like to set up a blank form for my users and then make this .xls
file into a template. However, when I do this, I get errors in copying
data on the worksheet_open. System behaves as if it doesnt know the
database file is even there.

The initialization code is nothing out of the ordinary... (defined this in
a separate module... body of the Workbook_Open event is
Call InitializeRoutines.InitialTableFill

Public Sub InitialTableFill()

Dim MyConnection As String
Dim MySQL As String
Dim MyDatabase As Object

Dim MyDatabaseFilePathAndName As String
MyDatabaseFilePathAndName = ThisWorkbook.Path & "\Statics.mdb"

'Create connection string

MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
MyConnection = MyConnection & "Data Source=" &
MyDatabaseFilePathAndName & ";"

Sheets("Statics").Range("A:D").ClearContents

MySQL = "Select [Company] FROM Company"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("A:A").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

'Create connection string

MySQL = "Select [Labor Category] FROM Labor ORDER BY [Labor Category]"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

Debug.Print "Inside the database query function ... connection seems good"

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("B:B").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

TTO_Request_Form.Activate

Exit Sub

SomethingWrong:
On Error GoTo 0
Set MyDatabase = Nothing
MsgBox "Error copying data", vbCritical, "Test Access data to Excel"

End Sub

If I do this as an .xlt file, it does *NOT* work. If I do this as an .xls
file it
works fine. Is it possible to make this a template ? I did humor the prompt
message from Microsoft asking to refresh the data every time it's opened.
This
method didnt work.

I'd appreciate any advice.

Thanks in advance,
Chad


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Excel Templates and databases

At least part of your problem is probably in this line

MyDatabaseFilePathAndName = ThisWorkbook.Path

when a new workbook is created from a template, it doesn't have a path since
it has never been saved.

--
Regards,
Tom Ogilvy


"ChadF" wrote:

I have a VBA / Excel application that does the the following:
in the Workbook_Open event, I query a MS Access database to
populate a hidden worksheet. (Have a number of named ranges
on my user's visible worksheet that are dependent on values that
I populate from the hidden.)

During the Worksheet_Change event, I have a function that queries
the same database file for lookup information once the user selects
some criterion.

I'd like to set up a blank form for my users and then make this .xls
file into a template. However, when I do this, I get errors in copying
data on the worksheet_open. System behaves as if it doesnt know the
database file is even there.

The initialization code is nothing out of the ordinary... (defined this in
a separate module... body of the Workbook_Open event is
Call InitializeRoutines.InitialTableFill

Public Sub InitialTableFill()

Dim MyConnection As String
Dim MySQL As String
Dim MyDatabase As Object

Dim MyDatabaseFilePathAndName As String
MyDatabaseFilePathAndName = ThisWorkbook.Path & "\Statics.mdb"

'Create connection string

MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
MyConnection = MyConnection & "Data Source=" &
MyDatabaseFilePathAndName & ";"

Sheets("Statics").Range("A:D").ClearContents

MySQL = "Select [Company] FROM Company"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("A:A").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

'Create connection string

MySQL = "Select [Labor Category] FROM Labor ORDER BY [Labor Category]"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

Debug.Print "Inside the database query function ... connection seems good"

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("B:B").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

TTO_Request_Form.Activate

Exit Sub

SomethingWrong:
On Error GoTo 0
Set MyDatabase = Nothing
MsgBox "Error copying data", vbCritical, "Test Access data to Excel"

End Sub

If I do this as an .xlt file, it does *NOT* work. If I do this as an .xls
file it
works fine. Is it possible to make this a template ? I did humor the prompt
message from Microsoft asking to refresh the data every time it's opened.
This
method didnt work.

I'd appreciate any advice.

Thanks in advance,
Chad


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Excel Templates and databases

Would something like this at the beginning of the workbook_open event correct
for this ? ...

before the call to initializeTableFill(),
ActiveWorkbook.Save
(force the save to something ?)




"Tom Ogilvy" wrote:

At least part of your problem is probably in this line

MyDatabaseFilePathAndName = ThisWorkbook.Path

when a new workbook is created from a template, it doesn't have a path since
it has never been saved.

--
Regards,
Tom Ogilvy


"ChadF" wrote:

I have a VBA / Excel application that does the the following:
in the Workbook_Open event, I query a MS Access database to
populate a hidden worksheet. (Have a number of named ranges
on my user's visible worksheet that are dependent on values that
I populate from the hidden.)

During the Worksheet_Change event, I have a function that queries
the same database file for lookup information once the user selects
some criterion.

I'd like to set up a blank form for my users and then make this .xls
file into a template. However, when I do this, I get errors in copying
data on the worksheet_open. System behaves as if it doesnt know the
database file is even there.

The initialization code is nothing out of the ordinary... (defined this in
a separate module... body of the Workbook_Open event is
Call InitializeRoutines.InitialTableFill

Public Sub InitialTableFill()

Dim MyConnection As String
Dim MySQL As String
Dim MyDatabase As Object

Dim MyDatabaseFilePathAndName As String
MyDatabaseFilePathAndName = ThisWorkbook.Path & "\Statics.mdb"

'Create connection string

MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
MyConnection = MyConnection & "Data Source=" &
MyDatabaseFilePathAndName & ";"

Sheets("Statics").Range("A:D").ClearContents

MySQL = "Select [Company] FROM Company"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("A:A").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

'Create connection string

MySQL = "Select [Labor Category] FROM Labor ORDER BY [Labor Category]"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

Debug.Print "Inside the database query function ... connection seems good"

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("B:B").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

TTO_Request_Form.Activate

Exit Sub

SomethingWrong:
On Error GoTo 0
Set MyDatabase = Nothing
MsgBox "Error copying data", vbCritical, "Test Access data to Excel"

End Sub

If I do this as an .xlt file, it does *NOT* work. If I do this as an .xls
file it
works fine. Is it possible to make this a template ? I did humor the prompt
message from Microsoft asking to refresh the data every time it's opened.
This
method didnt work.

I'd appreciate any advice.

Thanks in advance,
Chad


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default Excel Templates and databases

Tom, I answered my own question. Yes, doing an ActiveWorkbook.Save right in
the beginning solved the problem.

Appreciate your advice. (Was something I'd never considered before.)

What makes it a little more hairy - hard to debug since this is both a
template *and* a workbook_open event ...

Thanks again,
Chad


"ChadF" wrote:

Would something like this at the beginning of the workbook_open event correct
for this ? ...

before the call to initializeTableFill(),
ActiveWorkbook.Save
(force the save to something ?)




"Tom Ogilvy" wrote:

At least part of your problem is probably in this line

MyDatabaseFilePathAndName = ThisWorkbook.Path

when a new workbook is created from a template, it doesn't have a path since
it has never been saved.

--
Regards,
Tom Ogilvy


"ChadF" wrote:

I have a VBA / Excel application that does the the following:
in the Workbook_Open event, I query a MS Access database to
populate a hidden worksheet. (Have a number of named ranges
on my user's visible worksheet that are dependent on values that
I populate from the hidden.)

During the Worksheet_Change event, I have a function that queries
the same database file for lookup information once the user selects
some criterion.

I'd like to set up a blank form for my users and then make this .xls
file into a template. However, when I do this, I get errors in copying
data on the worksheet_open. System behaves as if it doesnt know the
database file is even there.

The initialization code is nothing out of the ordinary... (defined this in
a separate module... body of the Workbook_Open event is
Call InitializeRoutines.InitialTableFill

Public Sub InitialTableFill()

Dim MyConnection As String
Dim MySQL As String
Dim MyDatabase As Object

Dim MyDatabaseFilePathAndName As String
MyDatabaseFilePathAndName = ThisWorkbook.Path & "\Statics.mdb"

'Create connection string

MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
MyConnection = MyConnection & "Data Source=" &
MyDatabaseFilePathAndName & ";"

Sheets("Statics").Range("A:D").ClearContents

MySQL = "Select [Company] FROM Company"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("A:A").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

'Create connection string

MySQL = "Select [Labor Category] FROM Labor ORDER BY [Labor Category]"

' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1

Debug.Print "Inside the database query function ... connection seems good"

' Check to make sure we received data and copy the data

If Not MyDatabase.EOF Then

Sheets("Statics").Range("B:B").CopyFromRecordset MyDatabase

End If

MyDatabase.Close
Set MyDatabase = Nothing

TTO_Request_Form.Activate

Exit Sub

SomethingWrong:
On Error GoTo 0
Set MyDatabase = Nothing
MsgBox "Error copying data", vbCritical, "Test Access data to Excel"

End Sub

If I do this as an .xlt file, it does *NOT* work. If I do this as an .xls
file it
works fine. Is it possible to make this a template ? I did humor the prompt
message from Microsoft asking to refresh the data every time it's opened.
This
method didnt work.

I'd appreciate any advice.

Thanks in advance,
Chad


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
Excel- orgchart and databases mlha Charts and Charting in Excel 0 July 3rd 07 07:54 PM
Excel integration with databases JBruxer New Users to Excel 2 February 22nd 06 05:46 PM
Excel is opening templates as templates instead of as worksheets Desram Setting up and Configuration of Excel 1 January 19th 06 09:38 PM
Excel and Databases Stanley Excel Discussion (Misc queries) 4 December 28th 05 10:25 PM
Excel 2003, XML, databases and macros MarskiZ Excel Programming 0 July 28th 04 01:35 PM


All times are GMT +1. The time now is 09:51 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"