472,982 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Help with On Error GoTo

2
Hello,

I'm trying to use the following code to read data from Excel files and put the data into an MS Access Table. Some of the Excel files have the tabs in the sequence Chart1, Sheet1, Sheet2 etc while others have the order Sheet1, Sheet etc. The required information is on sheet1 in all cases. The line
Set xlsheet = xlwbook.Sheets.Item(1) specifies the first tab from the left with could be the chart or sheet1 depending on the file. If the first tab is actually a chart, then runtime error 13 (type mismatch) occurs. I thought that my On Error GoTo statement (which is located in the For loop that starts with For a = 0 To filecount - 1) would take care of this. What I hoped it would do is to go to the ErrHandler if the first tab is a chart and then change the parameter in the Item procedure to 2 so that it goes to sheet1. It works fine on the first execution of the loop, but on the second execution the ErrHandler doesnt even work and I get the runtime error 13. I know it should kick in the second time around since the first two files in the folder have chart before sheet1. Any ideas on why this is happening? Thanks AN

Option Compare Database

Sub LogTable()
'Creates a new table in the db1 database

Dim dat As DAO.Field
Dim shift As DAO.Field
Dim bls_day As DAO.Field
Dim db1 As DAO.Database
Dim MyTableDef As DAO.TableDef

' Make db1 the current DB.
Set db1 = DBEngine.Workspaces(0).Databases(0)

' Create new Table.
Set MyTableDef = db1.CreateTableDef("LogTable2")

' Create new Field.
Set dat = MyTableDef.CreateField("Date", dbDate)
Set shift = MyTableDef.CreateField("Shift", dbText)
Set bls_day = MyTableDef.CreateField("Bls/Day", dbText)

'Appends field to table's Fields Collection
MyTableDef.Fields.Append dat
MyTableDef.Fields.Append shift
MyTableDef.Fields.Append bls_day


' Add table to the collection.
db1.TableDefs.Append MyTableDef

'Get file names and place them in an array
Dim fso As New FileSystemObject
Dim fls As Files
Dim f As file
Dim fileNameArr(35) As String
Dim i As Integer
Dim path As String
Dim filecount As Integer
path = "C:\Documents and Settings\strat-temp\Desktop\TestFolder\Copy of Log Files"

Set fls = fso.GetFolder(path).Files

i = 0
For Each f In fls
fileNameArr(i) = path + "\" + f.Name
i = i + 1
Next

filecount = fls.Count 'Counts the number of files in the folder

'Get information from Excel Log Files and add to Table
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Dim row As Integer
Dim month As String
Dim dat1 As String 'dat1 stores the date of the Log File
Dim shift1 As String
Dim a As Integer
Dim blsdata As String

'Dim db1 As DAO.Database
Dim LogData As DAO.Recordset
Set db1 = CurrentDb()
Set rstLog = db1.OpenRecordset("LogTable2")

For a = 0 To filecount - 1
Set xlwbook = xl.Workbooks.Open(fileNameArr(a))

On Error GoTo ErrHandler
Set xlsheet = xlwbook.Sheets.Item(1) 'Specifies which sheet to look at
ErrHandler:
If Err = 13 Then
Set xlsheet = xlwbook.Sheets.Item(2)
Else
If Err <> 13 Then
Set xlsheet = xlwbook.Sheets.Item(1)
End If
End If

row = 1
Do Until xlsheet.Cells(row, 1) = "FRC-1 (Bls/day)"
blsdata = xlsheet.Cells(row, 1)
row = row + 1
Loop
blsdata = xlsheet.Cells(row, 2)
dat1 = xlsheet.Cells(3, 9)
shift1 = xlsheet.Cells(3, 3)
xlsheet.Parent.Close (False) 'Closes excel workbook

rstLog.AddNew
rstLog("Bls/Day") = blsdata
rstLog("Date") = dat1
rstLog("Shift") = shift1
rstLog.Update 'Saves the added record

Next a

'closing excel objects
Set xl = xlsheet.Parent.Parent ' Page 1082 of Access Book
xl.Quit

rstLog.Close 'Clean up object

End Sub
Jun 14 '07 #1
3 3870
hariharanmca
1,977 1GB
Hello,

I'm trying to use the following code to read data from Excel files and put the data into an MS Access Table. Some of the Excel files have the tabs in the sequence Chart1, Sheet1, Sheet2 etc while others have the order Sheet1, Sheet etc. The required information is on sheet1 in all cases. The line
Set xlsheet = xlwbook.Sheets.Item(1) specifies the first tab from the left with could be the chart or sheet1 depending on the file. If the first tab is actually a chart, then runtime error 13 (type mismatch) occurs. I thought that my On Error GoTo statement (which is located in the For loop that starts with For a = 0 To filecount - 1) would take care of this. What I hoped it would do is to go to the ErrHandler if the first tab is a chart and then change the parameter in the Item procedure to 2 so that it goes to sheet1. It works fine on the first execution of the loop, but on the second execution the ErrHandler doesnt even work and I get the runtime error 13. I know it should kick in the second time around since the first two files in the folder have chart before sheet1. Any ideas on why this is happening? Thanks AN

Option Compare Database

Sub LogTable()
'Creates a new table in the db1 database

Dim dat As DAO.Field
Dim shift As DAO.Field
Dim bls_day As DAO.Field
Dim db1 As DAO.Database
Dim MyTableDef As DAO.TableDef

' Make db1 the current DB.
Set db1 = DBEngine.Workspaces(0).Databases(0)

' Create new Table.
Set MyTableDef = db1.CreateTableDef("LogTable2")

' Create new Field.
Set dat = MyTableDef.CreateField("Date", dbDate)
Set shift = MyTableDef.CreateField("Shift", dbText)
Set bls_day = MyTableDef.CreateField("Bls/Day", dbText)

'Appends field to table's Fields Collection
MyTableDef.Fields.Append dat
MyTableDef.Fields.Append shift
MyTableDef.Fields.Append bls_day


' Add table to the collection.
db1.TableDefs.Append MyTableDef

'Get file names and place them in an array
Dim fso As New FileSystemObject
Dim fls As Files
Dim f As file
Dim fileNameArr(35) As String
Dim i As Integer
Dim path As String
Dim filecount As Integer
path = "C:\Documents and Settings\strat-temp\Desktop\TestFolder\Copy of Log Files"

Set fls = fso.GetFolder(path).Files

i = 0
For Each f In fls
fileNameArr(i) = path + "\" + f.Name
i = i + 1
Next

filecount = fls.Count 'Counts the number of files in the folder

'Get information from Excel Log Files and add to Table
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Dim row As Integer
Dim month As String
Dim dat1 As String 'dat1 stores the date of the Log File
Dim shift1 As String
Dim a As Integer
Dim blsdata As String

'Dim db1 As DAO.Database
Dim LogData As DAO.Recordset
Set db1 = CurrentDb()
Set rstLog = db1.OpenRecordset("LogTable2")

For a = 0 To filecount - 1
Set xlwbook = xl.Workbooks.Open(fileNameArr(a))

On Error GoTo ErrHandler
Set xlsheet = xlwbook.Sheets.Item(1) 'Specifies which sheet to look at
ErrHandler:
If Err = 13 Then
Set xlsheet = xlwbook.Sheets.Item(2)
Else
If Err <> 13 Then
Set xlsheet = xlwbook.Sheets.Item(1)
End If
End If

row = 1
Do Until xlsheet.Cells(row, 1) = "FRC-1 (Bls/day)"
blsdata = xlsheet.Cells(row, 1)
row = row + 1
Loop
blsdata = xlsheet.Cells(row, 2)
dat1 = xlsheet.Cells(3, 9)
shift1 = xlsheet.Cells(3, 3)
xlsheet.Parent.Close (False) 'Closes excel workbook

rstLog.AddNew
rstLog("Bls/Day") = blsdata
rstLog("Date") = dat1
rstLog("Shift") = shift1
rstLog.Update 'Saves the added record

Next a

'closing excel objects
Set xl = xlsheet.Parent.Parent ' Page 1082 of Access Book
xl.Quit

rstLog.Close 'Clean up object

End Sub




So you can use

Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
Jun 15 '07 #2
hariharanmca
1,977 1GB
So you can use

Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo ErrHandler 
Change To

Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
[/quote]


and remove the lable ErrHandler
Jun 15 '07 #3
alnug
2
That suggestion worked well. Thanks a lot.
Jun 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
37
by: Tim Marshall | last post by:
From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you...
2
by: Susan Bricker | last post by:
Greetings. Before I begin, I have been stuck on this problem for about a 5 days, now. I have tried and just seem to be not getting anywhere. I know that the explanation is lengthy, but I am a...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.