vba across access and
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » VBA across Access and Excel

VBA across Access and Excel

resolvedResolved · High Priority · Version 2003

replyReply Thu 12 Feb 2009, 10:43Delegate Monique said...

Monique has attended:
Excel VBA Intro Intermediate course

VBA across Access and Excel

I am currently writing a VBA macro in Access which opens and uses an Excel spreadsheet to import certain pieces of data. I am trying to use the Find method to find a piece of text in the Excel spreadsheet and, using the range returned, obtain the row that the piece of text is in (I know that there is only one instance of the text in the spreadsheet).

However, I receive a Compile error: User-defined type not defined on the Range variable which I have set to hold the result of the Find method. Is this because Access and Excel have different find methods? Or because I am trying to run an Excel VBA method from Access VBA?

I would be grateful if you would let me know if I am doing something wrong, or whether there is anyother way of achieving my purpose (ideally without using a for loop). I have attached a snippet of my code below which hopefully will show what I am doing.

Many thanks,

Monique

NOTE: xl is an Object variable holding the Excel application and spreadsheet to copy data from. Any undeclared variables are Public variables declared in the main module.

Private Sub Main2UploadTAs()

Dim UnitRange As Range
Dim UnitRow As Integer
Dim Column As Integer

xl.Sheets(1).Select
' The rows of data start at row 21
Row = 21
' While there is a new row of data
Do While Not IsEmpty(xl.Cells(Row, 11))
' Select the plant
Plant = xl.Cells(Row, 3)
' Select the data for that unit for that year
TAssay = xl.Cells(Row, 11)
*
This is the section of code which I am having problems with
*
' Select the sheet to search in
xl.Sheets("UNITS").Select
' Find the cell which contains the text in the Plant string
Set UnitRange = xl.Cells.Find(What:=Plant, After:=xl.Cells(1, 1), LookIn:=xl.xlValues, LookAt:= _
xl.xlPart, SearchOrder:=xl.xlByRows, SearchDirection:=xl.xlNext, MatchCase:=False _
, SearchFormat:=False)

UnitRow = UnitRange.Row
*
The code from this point on works
*
' The first unit ID is in column 5
Column = 5
Do While Not IsEmpty(xl.Cells(UnitRow, Column))
' Select the unit ID
UnitID = xl.Cells(UnitRow, Column)
' Update the record in the database
DoCmd.RunSQL "UPDATE ProductionUnitAssayCalendar SET [Assay]=" & TailsAssay & " WHERE [ProductionUnitId]=" & UnitID & " AND [TypeId]=1 AND [StartDate]=#01/01/" & Year & "#;"
' Move across the list of units
Column = Column + 1
Loop
' Move down the list of plants
Row = Row + 1
Loop
Next Year

End Sub


For upcoming training course dates see: Pricing & availability

replyReply Wed 18 Mar 2009, 10:32Trainer Stephen said...

RE: VBA across Access and Excel

Hi Monique

Thank you for your question.

The problem that you are facing is that you are trying to use the excel object model from within access. This is possible, but it requires a range of techniques that are quite involved (they take up to a couple of hours on our advanced VBA course.

In general you need to reference the excel object model by opening the VBE and clicking tools_references and selecting the "microsoft excel 12 object library" option. This will allow you to use excel objects in your code. The syntax is however different and too involved to discuss here. The following link provides a good overview

http://word.mvps.org/fAQs/InterDev/EarlyvsLateBinding.htm

Alternatively, I would recommend attanding a vba advanced course

Regards

Stephen

Tue 24 Mar 2009: Automatically marked as resolved.

 

Please browse our web site to find out more about
microsoft-access-course and other Microsoft training courses.

Access tip:

Deleting duplicate records from a table

You cannot delete records tables where there duplicate records. A way around this is to create a new table which wont hold the duplicates. and then deleting the old table.

1. Use a make-table query based on this table only. IMPORTANT - Ensure that you include all of the fields from your original table or you may loose data.

2. Open the query's property sheet by using VIEW, QUERY, PROPERTIES, and set the Unique Values property to Yes

3. Because you have selected the Unique Values to Yes when you run the query, Access creates a new table without duplicate records. You can now delete the old table and rename the new one.

View all Access hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard