Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default New record in table in Access from VBA in Excel

I have an excel file that that a customer uses to order product. I can have
up to 40 different files at on time. (eg. order1.xls,
order2.xls,..order40.xls) I have an Access database that I use to track jobs
at the shop that take the orders. I wouls like to place a button on the excel
sheet that when clicked would run code that would:

1. Open a new record in table "jobs" in acces.
2. Place information from the excel file into certain fields in the table.
(eg. order1.xls, cell C2=11252. I would like to place the following
info in field "description" in table "jobs" - "Vehicle# 11252"")
3. Place value from field "JobNumber" in table "jobs" into cell D1 on
order1.xls.

I have little experience with interapplication code. I copied and tried this
from a web page but it does not work:

Sub MakeJob()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data
Source=C:\Documents_ and Settings\jeremy.CREEDBILT\My Documents\Test Job.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Jobs", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
With rs
.AddNew ' create a new record
' add values to each field in the record
.fields("Date") = Now()
If Range("a2") = "chevrolet" Then
.fields("Customer") = "Courtesy Chevrolet"
Else
.fields("Customer") = "Five Star Ford"
End If
.fields("Description") = "Accessories for Pick up, APS veh# " &
Range _("A1").Value
' add more fields if necessary...
.Update ' stores the new record
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

This code is one I am using on a sample DB until I get the code figured
right. Cell references might be different. I get this error when I try to run
the code:

Compile error:
User-define type not defined.
and this part of the code is highlighted:
cn As ADODB.Connection

Can anyone help me with the code for this project?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default New record in table in Access from VBA in Excel

Sounds like it should be a fun project... As for the user defined function
error you probably need to reference the project to ADO. In the VBE click on
Tools - References - Browse the list to find Microsoft ADO (some version
thereof). This should cure your UDF error.

HTH

"JCanyoneer" wrote:

I have an excel file that that a customer uses to order product. I can have
up to 40 different files at on time. (eg. order1.xls,
order2.xls,..order40.xls) I have an Access database that I use to track jobs
at the shop that take the orders. I wouls like to place a button on the excel
sheet that when clicked would run code that would:

1. Open a new record in table "jobs" in acces.
2. Place information from the excel file into certain fields in the table.
(eg. order1.xls, cell C2=11252. I would like to place the following
info in field "description" in table "jobs" - "Vehicle# 11252"")
3. Place value from field "JobNumber" in table "jobs" into cell D1 on
order1.xls.

I have little experience with interapplication code. I copied and tried this
from a web page but it does not work:

Sub MakeJob()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data
Source=C:\Documents_ and Settings\jeremy.CREEDBILT\My Documents\Test Job.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Jobs", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
With rs
.AddNew ' create a new record
' add values to each field in the record
.fields("Date") = Now()
If Range("a2") = "chevrolet" Then
.fields("Customer") = "Courtesy Chevrolet"
Else
.fields("Customer") = "Five Star Ford"
End If
.fields("Description") = "Accessories for Pick up, APS veh# " &
Range _("A1").Value
' add more fields if necessary...
.Update ' stores the new record
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

This code is one I am using on a sample DB until I get the code figured
right. Cell references might be different. I get this error when I try to run
the code:

Compile error:
User-define type not defined.
and this part of the code is highlighted:
cn As ADODB.Connection

Can anyone help me with the code for this project?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default New record in table in Access from VBA in Excel

Thank you, There is the ADO reference you told me to look for. I have
multiple versions of Microsoft ActiveX DAta Objects 2.x library references
available. Should I select all of them also?

"Jim Thomlinson" wrote:

Sounds like it should be a fun project... As for the user defined function
error you probably need to reference the project to ADO. In the VBE click on
Tools - References - Browse the list to find Microsoft ADO (some version
thereof). This should cure your UDF error.

HTH

"JCanyoneer" wrote:

I have an excel file that that a customer uses to order product. I can have
up to 40 different files at on time. (eg. order1.xls,
order2.xls,..order40.xls) I have an Access database that I use to track jobs
at the shop that take the orders. I wouls like to place a button on the excel
sheet that when clicked would run code that would:

1. Open a new record in table "jobs" in acces.
2. Place information from the excel file into certain fields in the table.
(eg. order1.xls, cell C2=11252. I would like to place the following
info in field "description" in table "jobs" - "Vehicle# 11252"")
3. Place value from field "JobNumber" in table "jobs" into cell D1 on
order1.xls.

I have little experience with interapplication code. I copied and tried this
from a web page but it does not work:

Sub MakeJob()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data
Source=C:\Documents_ and Settings\jeremy.CREEDBILT\My Documents\Test Job.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Jobs", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
With rs
.AddNew ' create a new record
' add values to each field in the record
.fields("Date") = Now()
If Range("a2") = "chevrolet" Then
.fields("Customer") = "Courtesy Chevrolet"
Else
.fields("Customer") = "Five Star Ford"
End If
.fields("Description") = "Accessories for Pick up, APS veh# " &
Range _("A1").Value
' add more fields if necessary...
.Update ' stores the new record
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

This code is one I am using on a sample DB until I get the code figured
right. Cell references might be different. I get this error when I try to run
the code:

Compile error:
User-define type not defined.
and this part of the code is highlighted:
cn As ADODB.Connection

Can anyone help me with the code for this project?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default New record in table in Access from VBA in Excel

Select the most current one. If your users (assuming there are going to be
other users) have older versions of Excel, you may want to select an older
version of ADO to ensure backwards compatibility. That being said only select
one of them. Older versions will be less robust than newer versions.

HTH

"JCanyoneer" wrote:

Thank you, There is the ADO reference you told me to look for. I have
multiple versions of Microsoft ActiveX DAta Objects 2.x library references
available. Should I select all of them also?

"Jim Thomlinson" wrote:

Sounds like it should be a fun project... As for the user defined function
error you probably need to reference the project to ADO. In the VBE click on
Tools - References - Browse the list to find Microsoft ADO (some version
thereof). This should cure your UDF error.

HTH

"JCanyoneer" wrote:

I have an excel file that that a customer uses to order product. I can have
up to 40 different files at on time. (eg. order1.xls,
order2.xls,..order40.xls) I have an Access database that I use to track jobs
at the shop that take the orders. I wouls like to place a button on the excel
sheet that when clicked would run code that would:

1. Open a new record in table "jobs" in acces.
2. Place information from the excel file into certain fields in the table.
(eg. order1.xls, cell C2=11252. I would like to place the following
info in field "description" in table "jobs" - "Vehicle# 11252"")
3. Place value from field "JobNumber" in table "jobs" into cell D1 on
order1.xls.

I have little experience with interapplication code. I copied and tried this
from a web page but it does not work:

Sub MakeJob()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data
Source=C:\Documents_ and Settings\jeremy.CREEDBILT\My Documents\Test Job.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Jobs", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
With rs
.AddNew ' create a new record
' add values to each field in the record
.fields("Date") = Now()
If Range("a2") = "chevrolet" Then
.fields("Customer") = "Courtesy Chevrolet"
Else
.fields("Customer") = "Five Star Ford"
End If
.fields("Description") = "Accessories for Pick up, APS veh# " &
Range _("A1").Value
' add more fields if necessary...
.Update ' stores the new record
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

This code is one I am using on a sample DB until I get the code figured
right. Cell references might be different. I get this error when I try to run
the code:

Compile error:
User-define type not defined.
and this part of the code is highlighted:
cn As ADODB.Connection

Can anyone help me with the code for this project?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default New record in table in Access from VBA in Excel


Jim Thomlinson wrote:
If your users (assuming there are going to be
other users) have older versions of Excel, you may want to select an
older version of ADO to ensure backwards compatibility.


I think you are trying to apply advice you've heard about MS Office to
ADO. For example, if you set a reference to Excel9 (Excel2000) and open
it in ExcelXP on a machine without Excel2000, the reference will be
resolved to Excel10. The same does not apply to ADO. For example, if
you set a reference to ADO 2.1 and the machine only has ADO 2.8, the
result will be a missing reference. To handle this situation, use late
binding instead.

Older versions will be less robust than newer versions.


This is not necessarily true. I've heard that MDAC 2.5 (still available
for download) is much more stable than the more recent MDAC 2.7. The
most recent version, MDAC 2.8 (also available to download), is AFAIK
considered stable.

Jamie.

--



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default New record in table in Access from VBA in Excel


Fredrik Wahlgren wrote:
I have
multiple versions of Microsoft ActiveX DAta Objects 2.x library

references
available. Should I select all of them also?


No. Select ADO 2.8 which is the latest (and possibly last) version of

ADO.

Again, this will result in a missing reference for users who do not
have ADO 2.8.

The better approach in these circumstances is to use late binding e.g.

Dim cn As Object
Set cn = CreateObject("ADODB.Connection")

The version recordsed against "ADODB.Connection" in the registry will
be used. This will always be the most recent version of MDAC installed
on that machine i.e. will be ADO 2.8 if available.

Jamie.

--

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
How do I Consolidate an EXCEL Report into a record for ACCESS impo Mike M Excel Discussion (Misc queries) 1 September 16th 09 11:28 AM
Export Single Record to Append to Access Table RMS Excel Discussion (Misc queries) 1 December 9th 08 07:08 PM
Obtaining a single Access record from Excel input travis Links and Linking in Excel 5 November 23rd 05 09:02 AM
How can I update an MS Access table record from within Execl works David Canfield Excel Discussion (Misc queries) 0 January 14th 05 08:51 PM
Modifying an Access record in Excel John Moll Excel Programming 1 August 1st 03 05:27 PM


All times are GMT +1. The time now is 12:01 AM.

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"