Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Method '~' of object '~' failed

Status
Not open for further replies.

JoPaBC

Technical User
Sep 26, 2017
85
0
0
CA
Hello,
I have a VB6 program which used to be run on XP and it still works there without any issues. It basically reads Excel data, writes them to MS Access and saves MS Access data into TXT file.

We are moving from XP to Windows 10 and trouble started now - it starts OK, can connect to MS Access, but when trying to read Excel data it gives this error (tried to run in both Win XP SP 3 and Windows 8 compatibility mode)
untitled_ccwyua.png


Here is the code causing trouble:
Code:
Private Sub Command3_Click()
    Dim RATE, RT, OT, DT, DR, SH, WHR, WHRMAX, D15, REMRT As Double
    Dim JOBID, RTYPE, WTYPE, STR1, EMPNAME, Msg As String
    Dim i, J, COL, PPID, PDAYS, WNUM, EMPID, FIB, LL As Integer
    Dim STD, FD, NSTD, JUL1, AUG2, SEP3, MAY4, DM30, DM1 As Date
    Dim CIARKA, KLK, K As Integer
    Dim numericCheck As Boolean
    Dim SVIATKY() As String
    Dim WSVIATKY() As String
    Dim rst, rst1, rst2, rst3, rst4 As ADODB.Recordset

    On Error GoTo NAV1

    If Combo1.Text = "Select Pay Period" Then
        MsgBox "Select Pay Period!"
        Exit Sub
    End If

    If Len(Text1.Text) < 2 Then
        MsgBox "Select input file!"
        Exit Sub
    End If

    Me.MousePointer = vbHourglass

    sSQL = "Select [SH] From [STATS];"
    Set rst4 = New ADODB.Recordset
    rst4.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic

    KLK = rst4.RecordCount

    ReDim SVIATKY(1 To KLK) As String
    ReDim WSVIATKY(1 To KLK) As String

    For K = 1 To KLK
        SVIATKY(K) = rst4.Fields(0).Value
        WSVIATKY(K) = Format(SVIATKY(K), "ww", vbMonday)
        rst4.MoveNext
    Next

    sSQL = "Select [PPID], [PDAYS], [STD] From PAYPER where [PAYPER]='" & Combo1.Text & "';"
    Set rst = New ADODB.Recordset
    rst.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic
    PPID = rst.Fields(0).Value    'PAY PERIOD id
    PDAYS = rst.Fields(1).Value  'NUMBER OF DAYS IN PAY PERIOD
    NSTD = rst.Fields(2).Value  'FIRST DAY OF PAY PERIOD

    Set xlApp1 = New Excel.Application
    Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
    Set ws1 = wb1.Worksheets(1)


    For i = 6 To 999    'READ EXCEL ROWS

        EMPID = ws1.Cells(i, 1)  'EMPLOYEE ID NUMBER

        If IsNumeric(EMPID) = False Then
            GoTo NAV2
        End If

        EMPNAME = ws1.Cells(i, 2)    'EMPLOYEE NAME
        RATE = ws1.Cells(i, 3)   'PAY RATE [$]
        JOBID = Left(ws1.Cells(i, 4), Len(ws1.Cells(i, 4)) - 1)    'JOB ID + RATE TYPE H/D
        RTYPE = Right(ws1.Cells(i, 4), 1)  'RATE TYPE H/D

        COL = 6
        STD = NSTD    'SET THE FIRST DAY

        Label5.Caption = "Processing: " & EMPNAME
        Label5.Refresh

        sSQL = "Select [EMPID], [FULL_NAME] from [NAMES] where [EMPID]=" & EMPID & ";"
        Set rst1 = New ADODB.Recordset
        rst1.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic

        If rst1.RecordCount = 0 Then
            MsgBox "Name " & EMPNAME & " not in database!"
            Me.MousePointer = vbDefault
            Exit Sub
        End If

        CIARKA = InStr(rst1.Fields(1).Value, ",")

        If CIARKA = 0 Then
            CIARKA = 4
        End If

        If Right(EMPNAME, 1) = " " Then    'REMOVE RIGHT SPACE
            EMPNAME = Left(EMPNAME, Len(EMPNAME) - 1)
        End If

        If ((UCase(Left(rst1.Fields(1).Value, CIARKA - 1)) <> UCase(Left(EMPNAME, CIARKA - 1))) Or (UCase(Right(rst1.Fields(1).Value, 3)) <> UCase(Right(EMPNAME, 3)))) Then    'VERIFY NAMES
            MsgBox "Name '" & EMPNAME & "' and '" & rst1.Fields(1).Value & "' and " & rst1.Fields(0).Value & " do not match!"
            Me.MousePointer = vbDefault
            Exit Sub
        End If

        If RTYPE = "H" Then  'HOURLY RATE

            For J = 1 To PDAYS    'READ COLUMNS

                WNUM = Format(STD, "ww", vbMonday)

                If IsInArray(WSVIATKY, WNUM) = True Then
                    WHRMAX = 32
                Else
                    WHRMAX = 40
                End If

                If IsNumeric((ws1.Cells(i, COL))) = False Then    'valid time value
                    ws1.Cells(i, COL) = 0
                    ws1.Cells(i, COL).Interior.ColorIndex = 6
                    'GoTo nav4
                End If

                SH = 0    'RESET STAT HOLIDAY

                If IsInArray(SVIATKY, STD) = True Then

                    sSQL = "Select  Min(WDATE) From WHOURS Where [EMPID]=" & EMPID & ";"
                    Set rst = New ADODB.Recordset
                    rst.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic
                    FD = rst.Fields(0).Value

                    DM30 = STD - 30
                    DM1 = STD - 1

                    sSQL = "SELECT Count([WDATE]) From WHOURS Where [EMPID]=" & EMPID & " AND [WDATE] Between #" & DM30 & "# And #" & DM1 & "#;"
                    Set rst = New ADODB.Recordset
                    rst.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic
                    D15 = rst.Fields(0).Value

                    If (((STD - FD) >= 30) And D15 >= 15) Then  'EMPLOYED MORE THAN 30 DAYS, GETS EXTRA DAY

                        WTYPE = "SH"

                        sSQL = "SELECT * From WHOURS Where EMPID=" & EMPID & " AND WDATE= #" & STD & "# AND WTYPE=""" & WTYPE & """;"
                        Set rst3 = New ADODB.Recordset
                        rst3.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic

                        If rst3.RecordCount = 0 Then

                            If (ws1.Cells(i, COL) <= 12) Then    'OT
                                SH = 8
                                RT = 0
                                OT = ws1.Cells(i, COL)
                                DT = 0
                            End If
                            If (ws1.Cells(i, COL) > 12) Then    'OT + DT
                                SH = 8
                                RT = 0
                                OT = 12
                                DT = ws1.Cells(i, COL) - 12
                            End If
                        Else
                            If (ws1.Cells(i, COL) <= 12) Then    'OT
                                SH = 0
                                RT = 0
                                OT = ws1.Cells(i, COL)
                                DT = 0
                            End If
                            If (ws1.Cells(i, COL) > 12) Then    'OT + DT
                                SH = 0
                                RT = 0
                                OT = 12
                                DT = ws1.Cells(i, COL) - 12
                            End If

                        End If

                    Else   'EMPLOYED LESS THAN 30 DAYS, DON'T GET EXTRA DAY
                        If (ws1.Cells(i, COL) <= 12) Then    'OT
                            RT = 0
                            OT = ws1.Cells(i, COL)
                            DT = 0
                        End If
                        If (ws1.Cells(i, COL) > 12) Then    'OT + DT
                            RT = 0
                            OT = 12
                            DT = ws1.Cells(i, COL) - 12
                        End If
                    End If

                Else

                    If ((Weekday(STD) >= 2) And (Weekday(STD) <= 5)) Then    'IF MONDAY-THURSDAY
                        If ws1.Cells(i, COL) <= 8 Then    'RT
                            RT = ws1.Cells(i, COL)
                            OT = 0
                            DT = 0
                        End If
                        If ((ws1.Cells(i, COL) > 8) And (ws1.Cells(i, COL) <= 12)) Then    'RT + OT
                            RT = 8
                            OT = ws1.Cells(i, COL) - 8
                            DT = 0
                        End If
                        If (ws1.Cells(i, COL) > 12) Then    'RT + OT + DT
                            RT = 8
                            OT = 4
                            DT = ws1.Cells(i, COL) - 12
                        End If
                    End If


                    If (Weekday(STD) = 1) Then     'IF SUNDAY
                        If (ws1.Cells(i, COL) <= 12) Then    'OT
                            RT = 0
                            OT = ws1.Cells(i, COL)
                            DT = 0
                        End If
                        If (ws1.Cells(i, COL) > 12) Then    'OT + DT
                            RT = 0
                            OT = 12
                            DT = ws1.Cells(i, COL) - 12
                        End If
                    End If

                    If ((Weekday(STD) = 6) Or (Weekday(STD) = 7)) Then   'IF SATURDAY OR FRIDAY

                        STR1 = "R/T"
                        sSQL = "SELECT SUM([WHRS]) From WHOURS where ([WTYPE]='" & STR1 & "'  AND [EMPID]=" & EMPID & " AND [WEEKN]=" & WNUM & ");"
                        Set rst2 = New ADODB.Recordset
                        rst2.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic

                        If IsNull(rst2.Fields(0).Value) Then
                            WHR = 0
                        Else
                            WHR = rst2.Fields(0).Value
                        End If

                        If WHR >= WHRMAX Then       'REACHED 40/32 R/T IN WEEK
                            If (ws1.Cells(i, COL) <= 12) Then    'OT
                                RT = 0
                                OT = ws1.Cells(i, COL)
                                DT = 0
                            End If
                            If (ws1.Cells(i, COL) > 12) Then    'OT + DT
                                RT = 0
                                OT = 12
                                DT = ws1.Cells(i, COL) - 12
                            End If
                        Else                     'NOT REACHED 40/32 R/T IN WEEK YET
                            REMRT = WHRMAX - WHR
                            If REMRT >= 8 Then
                                If ws1.Cells(i, COL) <= 8 Then    'RT
                                    RT = ws1.Cells(i, COL)
                                    OT = 0
                                    DT = 0
                                End If
                                If ((ws1.Cells(i, COL) > 8) And (ws1.Cells(i, COL) <= 12)) Then    'RT + OT
                                    RT = 8
                                    OT = ws1.Cells(i, COL) - 8
                                    DT = 0
                                End If
                                If (ws1.Cells(i, COL) > 12) Then    'RT + OT + DT
                                    RT = 8
                                    OT = 4
                                    DT = ws1.Cells(i, COL) - 12
                                End If
                            Else
                                If ws1.Cells(i, COL) <= REMRT Then    'RT LESS THAN 8 HRS
                                    RT = ws1.Cells(i, COL)
                                    OT = 0
                                    DT = 0
                                End If
                                If ((ws1.Cells(i, COL) > REMRT) And (ws1.Cells(i, COL) <= 12)) Then    'RT + OT
                                    RT = REMRT
                                    OT = ws1.Cells(i, COL) - REMRT
                                    DT = 0
                                End If
                                If (ws1.Cells(i, COL) > 12) Then    'RT + OT + DT
                                    RT = REMRT
                                    OT = 12 - REMRT
                                    DT = ws1.Cells(i, COL) - 12
                                End If
                            End If
                        End If
                    End If
                End If

                If RT > 0 Then
                    WTYPE = "R/T"
                    sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & RT & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                    Set rsAccess = New ADODB.Recordset
                    rsAccess.CursorLocation = adUseClient
                    rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText
                End If

                If OT > 0 Then
                    WTYPE = "O/T"
                    sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & OT & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                    Set rsAccess = New ADODB.Recordset
                    rsAccess.CursorLocation = adUseClient
                    rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText
                End If

                If DT > 0 Then
                    WTYPE = "D/T"
                    sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & DT & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                    Set rsAccess = New ADODB.Recordset
                    rsAccess.CursorLocation = adUseClient
                    rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText
                End If

                If SH > 0 Then
                    WTYPE = "SH"
                    sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & SH & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                    Set rsAccess = New ADODB.Recordset
                    rsAccess.CursorLocation = adUseClient
                    rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText

                End If

                COL = COL + 1
                STD = DateAdd("d", 1, STD)
            Next J

        Else  'DAY RATE

            For J = 1 To PDAYS

                If IsNumeric((ws1.Cells(i, COL))) = False Then    'valid time value
                    ws1.Cells(i, COL) = 0
                    ws1.Cells(i, COL).Interior.ColorIndex = 6
                    'GoTo nav4
                End If

                '*******************
                SH = 0    'RESET STAT HOLIDAY

                If IsInArray(SVIATKY, STD) = True Then

                    sSQL = "Select  Min(WDATE) From WHOURS Where [EMPID]=" & EMPID & ";"
                    Set rst = New ADODB.Recordset
                    rst.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic
                    FD = rst.Fields(0).Value

                    DM30 = STD - 30
                    DM1 = STD - 1

                    sSQL = "SELECT Count([WDATE]) From WHOURS Where [EMPID]=" & EMPID & " AND [WDATE] Between #" & DM30 & "# And #" & DM1 & "#;"
                    Set rst = New ADODB.Recordset
                    rst.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic
                    D15 = rst.Fields(0).Value

                    If (((STD - FD) >= 30) And D15 >= 15) Then  'EMPLOYED MORE THAN 30 DAYS, GETS EXTRA DAY

                        If JOBID = 20 Then  'STAT HOLIDAY FOR COOKS/KITCHEN STAFF
                            DR = 1
                            WTYPE = "S/T"

                            sSQL = "SELECT * From WHOURS Where EMPID=" & EMPID & " AND WDATE= #" & STD & "# AND WTYPE=""" & WTYPE & """;"
                            Set rst3 = New ADODB.Recordset
                            rst3.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic

                            If rst3.RecordCount = 0 Then
                                sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & DR & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                                Set rsAccess = New ADODB.Recordset
                                rsAccess.CursorLocation = adUseClient
                                rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText
                            End If

                        End If
                    End If

                End If

                '*******************

                WNUM = Format(STD, "ww", vbMonday)
                DR = ws1.Cells(i, COL)
                If DR > 0 Then
                    If JOBID = 15 Then
                        WTYPE = "FAB"
                    Else
                        WTYPE = "T/T"
                    End If
                    sSQL = "INSERT INTO WHOURS (EMPID, WDATE, WTYPE, WHRS, PCODE, WRATE, WEEKN, PAYPER) VALUES ('" & EMPID & "', '" & STD & "',  '" & WTYPE & "',  '" & DR & "', '" & JOBID & "', '" & RATE & "', '" & WNUM & "', '" & PPID & "'); "
                    Set rsAccess = New ADODB.Recordset
                    rsAccess.CursorLocation = adUseClient
                    rsAccess.Open sSQL, objAccessConnection, adOpenKeyset, adLockOptimistic, adCmdText
                End If
                COL = COL + 1
                STD = DateAdd("d", 1, STD)

            Next J
        End If
    Next i

NAV2:

    Label5.Caption = " "
    Label5.Refresh

    Me.MousePointer = vbDefault

    xlApp1.AlertBeforeOverwriting = False
    xlApp1.DisplayAlerts = False

    Set ws1 = Nothing
    wb1.Save
    wb1.Close
    Set wb1 = Nothing
    Set xlApp1 = Nothing

    MsgBox "Done"

    Exit Sub
NAV1:
    Msg = "Error # " & str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End Sub

Any idea how to fix this, please?
 
Sorry for getting this threat that long but maybe we are getting closer...

I tried to run a smaller project on Windows 10, here is the error

Untitled_pxyr0k.png


here is the whole code

Code:
Option Explicit

Dim xlApp1 As Excel.Application
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim xlApp2 As Excel.Application
Dim wb2 As Workbook
Dim ws2 As Worksheet

Private Sub Command1_Click()
          Dim Filter As String

10        Filter = "Excel Files (*.xls)|*.xls;|"
20        Filter = Filter + "All Formats(*.*)|*.mde;*.mdb;|"

30        CommonDialog1.Filter = Filter
40        CommonDialog1.ShowOpen
50        CommonDialog1.FilterIndex = 1

60        Text1.Text = CommonDialog1.FileName

End Sub

Private Sub Command2_Click()
          Dim Filter As String

70        Filter = "Excel Files (*.xls)|*.xls;|"
80        Filter = Filter + "All Formats(*.*)|*.mde;*.mdb;|"

90        CommonDialog2.Filter = Filter
100       CommonDialog2.ShowOpen
110       CommonDialog2.FilterIndex = 1

120       Text2.Text = CommonDialog2.FileName

End Sub

Private Sub Command3_Click()
      'COMPARE LUMBER INVENTORY
          Dim i, j As Integer
          Dim Msg As String

10        On Error GoTo NAV1

20        If Len(Text1.Text) < 2 Then
30            MsgBox "Select first file!"
40            Exit Sub
50        End If

60        If Len(Text2.Text) < 2 Then
70            MsgBox "Select second file!"
80            Exit Sub
90        End If

100       Set xlApp1 = New Excel.Application
110       Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
120       Set ws1 = wb1.Worksheets(1)

130       Set xlApp2 = New Excel.Application
140       Set wb2 = xlApp2.Workbooks.Open(Text2.Text)
150       Set ws2 = wb2.Worksheets(1)

160       Me.MousePointer = vbHourglass

170       For i = 1 To Text3.Text
180           Label1.Caption = i
190           If (ws2.Cells(i, 7) = "GRN" Or ws2.Cells(i, 7) = "KD" Or ws2.Cells(i, 7) = "KDOS" Or ws2.Cells(i, 7) = "HC" Or ws2.Cells(i, 7) = "FOHC") Then
200               For j = 1 To Text3.Text
210                   Label2.Caption = j
220                   Label2.Refresh
230                   If ws1.Cells(j, 1) = ws2.Cells(i, 1) Then
240                       If ws1.Cells(j, 8) = ws2.Cells(i, 8) Then
250                           ws2.Cells(i, 12) = ws1.Cells(j, 12)
260                       Else
270                           ws2.Cells(i, 12) = ws1.Cells(j, 12) / ws1.Cells(j, 8) * ws2.Cells(i, 8)
280                       End If
290                       Exit For    'new line
300                   End If
310               Next j
320           End If
330       Next i

340       Label2.Caption = ""
350       Label2.Refresh

360       Set ws1 = Nothing
370       wb1.Save
380       wb1.Close
390       Set wb1 = Nothing
400       Set xlApp1 = Nothing

410       Set ws2 = Nothing
420       wb2.Save
430       wb2.Close
440       Set wb2 = Nothing
450       Set xlApp2 = Nothing
          
460       Excel.Application.Quit
470       Excel.Application.Quit

480       Me.MousePointer = vbDefault

490       MsgBox "Done"

NAV1:
500       Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
510       MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext

End Sub

Private Sub Command4_Click()
620       On Error Resume Next

630       xlApp1.AlertBeforeOverwriting = False
640       xlApp1.DisplayAlerts = False
              
650       Set ws1 = Nothing
660       wb1.Close
670       Set wb1 = Nothing
680       Set xlApp1 = Nothing
690       Set ws2 = Nothing
700       wb2.Close
710       Set wb2 = Nothing
720       Set xlApp2 = Nothing
          
730       Excel.Application.Quit
740       Excel.Application.Quit

750       End

End Sub

Private Sub Command5_Click()
      'COMPARE LUMBER SALES
          Dim i, j As Integer
          Dim Msg As String

10        On Error GoTo NAV1

20        If Len(Text1.Text) < 2 Then
30            MsgBox "Select first file!"
40            Exit Sub
50        End If

60        If Len(Text2.Text) < 2 Then
70            MsgBox "Select second file!"
80            Exit Sub
90        End If

100       Set xlApp1 = New Excel.Application
110       Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
120       Set ws1 = wb1.Worksheets(1)

130       Set xlApp2 = New Excel.Application
140       Set wb2 = xlApp2.Workbooks.Open(Text2.Text)
150       Set ws2 = wb2.Worksheets(1)

160       Me.MousePointer = vbHourglass

170       For i = 44 To Text3.Text

180           If ws1.Cells(i, 6) = "RGH" Then
190               ws1.Cells(i, 1).Font.ColorIndex = 3       'red
200               For j = 44 To Text3.Text

210                   If ws2.Cells(j, 1) = ws1.Cells(i, 1) Then
220                       If ws1.Cells(i, 8) = ws2.Cells(j, 8) Then
230                           ws1.Cells(i, 1).Font.ColorIndex = 5    'blue
240                       Else
250                           ws1.Cells(i, 1).Font.ColorIndex = 10    'green
260                       End If

270                   End If
280               Next j
290           End If
300       Next i
          
310       Set ws1 = Nothing
320       wb1.Save
330       wb1.Close
340       Set wb1 = Nothing
350       Set xlApp1 = Nothing
        
360       Set ws2 = Nothing
370       wb2.Save
380       wb2.Close
390       Set wb2 = Nothing
400       Set xlApp2 = Nothing
          
410       Excel.Application.Quit
420       Excel.Application.Quit

430       Me.MousePointer = vbDefault

440       MsgBox "Done"

NAV1:
450       Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
460       MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End Sub

Private Sub Form_Unload(Cancel As Integer)
1210      On Error Resume Next

1220      xlApp1.AlertBeforeOverwriting = False
1230      xlApp1.DisplayAlerts = False

1240      Set ws1 = Nothing
1250      wb1.Close
1260      Set wb1 = Nothing
1270      Set xlApp1 = Nothing
1280      Set ws2 = Nothing
1290      wb2.Close
1300      Set wb2 = Nothing
1310      Set xlApp2 = Nothing
          
1320      Excel.Application.Quit
1330      Excel.Application.Quit

End Sub

It looks like this line is causing the error(?):

Code:
 Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
 
In that latest code you are NOT late binding. And that is certainly the cause of the error in this case.
 
Well, late binding version is as the following:

late_hprkjf.png

22_a7qas5.png


code:

Code:
Option Explicit

Dim xlApp1 As Object
Dim wb1 As Object
Dim ws1 As Object
Dim xlApp2 As Object
Dim wb2 As Object
Dim ws2 As Object

Private Sub Command1_Click()
          Dim Filter As String

10        Filter = "Excel Files (*.xls)|*.xls;|"
20        Filter = Filter + "All Formats(*.*)|*.mde;*.mdb;|"

30        CommonDialog1.Filter = Filter
40        CommonDialog1.ShowOpen
50        CommonDialog1.FilterIndex = 1

60        Text1.Text = CommonDialog1.FileName

End Sub

Private Sub Command2_Click()
          Dim Filter As String

70        Filter = "Excel Files (*.xls)|*.xls;|"
80        Filter = Filter + "All Formats(*.*)|*.mde;*.mdb;|"

90        CommonDialog2.Filter = Filter
100       CommonDialog2.ShowOpen
110       CommonDialog2.FilterIndex = 1

120       Text2.Text = CommonDialog2.FileName

End Sub

Private Sub Command3_Click()
      'COMPARE LUMBER INVENTORY
          Dim i, j As Long
          Dim Msg As String

10        On Error GoTo NAV1

20        If Len(Text1.Text) < 2 Then
30            MsgBox "Select first file!"
40            Exit Sub
50        End If

60        If Len(Text2.Text) < 2 Then
70            MsgBox "Select second file!"
80            Exit Sub
90        End If

100       Set xlApp1 = CreateObject("Excel.Application")
110       Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
120       Set ws1 = wb1.Worksheets(1)

130       Set xlApp2 = CreateObject("Excel.Application")
140       Set wb2 = xlApp2.Workbooks.Open(Text2.Text)
150       Set ws2 = wb2.Worksheets(1)

160       Me.MousePointer = vbHourglass

170       For i = 1 To Text3.Text
180           Label1.Caption = i

190           If (ws2.Cells(i, 7) = "GRN" Or ws2.Cells(i, 7) = "KD" Or ws2.Cells(i, 7) = "KDOS" Or ws2.Cells(i, 7) = "HC" Or ws2.Cells(i, 7) = "FOHC") Then
200               For j = 1 To Text3.Text
210                   Label2.Caption = j
220                   Label2.Refresh
230                   If ws1.Cells(j, 1) = ws2.Cells(i, 1) Then
240                       If ws1.Cells(j, 8) = ws2.Cells(i, 8) Then

250                           ws2.Cells(i, 12) = ws1.Cells(j, 12)

260                       Else
270                           ws2.Cells(i, 12) = ws1.Cells(j, 12) / ws1.Cells(j, 8) * ws2.Cells(i, 8)
280                       End If
290                       Exit For    'new line
300                   End If
310               Next j
320           End If
330       Next i

340       Label2.Caption = ""
350       Label2.Refresh

360       Set ws1 = Nothing
370       wb1.Save
380       wb1.Close
390       Set wb1 = Nothing
400       Set xlApp1 = Nothing

410       Set ws2 = Nothing
420       wb2.Save
430       wb2.Close
440       Set wb2 = Nothing
450       Set xlApp2 = Nothing
          'Excel.Application.Quit
          'Excel.Application.Quit

460       Me.MousePointer = vbDefault

470       MsgBox "Done"
NAV1:
480       Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
490       MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
500   End Sub


Private Sub Command4_Click()
          'On Error Resume Next

590       xlApp1.AlertBeforeOverwriting = False
600       xlApp1.DisplayAlerts = False
              
610       Set ws1 = Nothing
620       wb1.Close
630       Set wb1 = Nothing
640       Set xlApp1 = Nothing
650       Set ws2 = Nothing
660       wb2.Close
670       Set wb2 = Nothing
680       Set xlApp2 = Nothing
          'Excel.Application.Quit
          'Excel.Application.Quit

690       End

End Sub

Private Sub Command5_Click()
      'COMPARE LUMBER SALES
          Dim i, j As Long
          Dim Msg As String

700                 On Error GoTo NAV1

710       If Len(Text1.Text) < 2 Then
720           MsgBox "Select first file!"
730           Exit Sub
740       End If

750       If Len(Text2.Text) < 2 Then
760           MsgBox "Select second file!"
770           Exit Sub
780       End If

          'Set xlApp1 = New Excel.Application
790       Set xlApp1 = CreateObject("Excel.Application")
800       Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
810       Set ws1 = wb1.Worksheets(1)

          'Set xlApp2 = New Excel.Application
820       Set xlApp2 = CreateObject("Excel.Application")
830       Set wb2 = xlApp2.Workbooks.Open(Text2.Text)
840       Set ws2 = wb2.Worksheets(1)

850       Me.MousePointer = vbHourglass

860       For i = 44 To Text3.Text

870           If ws1.Cells(i, 6) = "RGH" Then
880               ws1.Cells(i, 1).Font.ColorIndex = 3       'red
890               For j = 44 To Text3.Text

900                   If ws2.Cells(j, 1) = ws1.Cells(i, 1) Then
910                       If ws1.Cells(i, 8) = ws2.Cells(j, 8) Then
920                           ws1.Cells(i, 1).Font.ColorIndex = 5    'blue
930                       Else
940                           ws1.Cells(i, 1).Font.ColorIndex = 10    'green
950                       End If

960                       Exit For
970                   End If
980               Next j
990           End If
1000      Next i
          
1010      Set ws1 = Nothing
1020      wb1.Save
1030      wb1.Close
1040      Set wb1 = Nothing
1050      Set xlApp1 = Nothing
        
1060      Set ws2 = Nothing
1070      wb2.Save
1080      wb2.Close
1090      Set wb2 = Nothing
1100      Set xlApp2 = Nothing
          'Excel.Application.Quit
          'Excel.Application.Quit

1110      Me.MousePointer = vbDefault

1120      MsgBox "Done"
NAV1:
    Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
    MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End Sub


Private Sub Form_Unload(Cancel As Integer)
          'On Error Resume Next

1130      xlApp1.AlertBeforeOverwriting = False
1140      xlApp1.DisplayAlerts = False

1150      Set ws1 = Nothing
1160      wb1.Close
1170      Set wb1 = Nothing
1180      Set xlApp1 = Nothing
1190      Set ws2 = Nothing
1200      wb2.Close
1210      Set wb2 = Nothing
1220      Set xlApp2 = Nothing
          'Excel.Application.Quit
          'Excel.Application.Quit

End Sub
 
>I have two Excel (2000 and 2007) versions installed on Windows 10, only Excel 2000 on XP; note sure if that can make any difference.

Sadly yes, it makes difference, if you are using early binding.
 
Well, for the early binding I am using the "Microsoft Excel 9.0 Object Library" (should work for Excel 2000 in both XP and Windows 10 as both have Excel 2000).

However, the late binding version (I hope I used it correctly) gives me the same error on Windows 10 (both versions work on XP).
 
>early binding … should work for Excel 2000 in both XP and Windows 10 as both have Excel 2000

Nope, as I say, sadly not. Doesn't work quite the way you think it does.

Basically, COM (and thus ActiveX and automation) uses a CLSID to determine the binary file that contains the object (by looking it up in the registry), i.e. the relevant automation server. But all versions of Excel from 2000 onwards have the same CLSID, and it is only the most recent one installed (or, more accurately, the one that was most recently registered) that will be found in this lookup.

So, you can search for Excel.Application.9 (this is the PROGID for "Microsoft Excel 9.0 Object Library") under HKEY_CLASSES_ROOT in the registry. Under that key, you will find a subkey called CLSID. Do a search for that CLSID. Under that CLSID you will find a subkey called LocalServer32, and that key contains the full path to the automation server (in this case Excel) that will actually be used. On the machines that have Excel 2000 and 2007, this will be the 2007 version. With early binding we then run into problems as we end up doing the COM equivalent of making function calls via hard-coded addresses, in a library where the functions are not at those addresses

However, if the late binding version is not working then we may have a different (although possibly related) issue. Trouble is that I've now lost track of what code you are actually using.
 
Do yourself (and us) a favor - write a small, simple VB6 app that just starts Excel and opens a file. And test using it. No additional logic. The code will be just a few lines and you will have a better chance on solving your issue.

Something like this should do (one form, one command button):

Code:
Option Explicit

Private Sub Command1_Click()
Dim xlApp1 As Object
Dim wb1 As Object
Dim ws1 As Object

Set xlApp1 = CreateObject("Excel.Application")
xlApp1.Visible = True
Set wb1 = xlApp1.Workbooks.Open([red]"C:\TEMP\TestFile.xlsx"[/red])
Set ws1 = wb1.Worksheets(1)

Set ws1 = Nothing
Set wb1 = Nothing
Set xlApp1 = Nothing

End Sub

---- Andy

There is a great need for a sarcasm font.
 
Thanks for your patience.
I couldn't find this entry "Under that CLSID you will find a subkey called LocalServer32"
I found "Excel.Application.12" in "HKEY_CLASSES_ROOT\Excel.Application\CurVer" (Windows 10)

This small VB6 code works okay (it just opens a specific excel file) on both Windows XP and Windows 10

Code:
Option Explicit

Dim xlApp1 As Object
Dim wb1 As Object
Dim ws1 As Object

Private Sub Command5_Click()

10    On Error GoTo ErrHandler
          Dim xlApp As Object
          Dim xlWB As Object
          Dim Msg As String
          
20        Set xlApp = CreateObject("Excel.Application")
30        xlApp.Visible = True
          
40        Set xlWB = xlApp.Workbooks.Open("c:\test.xls")
50        Exit Sub
          
ErrHandler:
60        Msg = "Error # " & Str(Err.Number) & " was generated by " _
        & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
70        MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
          
End Sub

Private Sub Form_Unload(Cancel As Integer)
                'exit

80          Set ws1 = Nothing
90          Set wb1 = Nothing
100         Set xlApp1 = Nothing

End Sub
 
>I couldn't find this entry "Under that CLSID you will find a subkey called LocalServer32"

Er … it isn't in the same place. As advised, you'll have to search the registry using the Find feature

On my PC, it is located here:

HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{00024500-0000-0000-C000-000000000046

CLSID_cuu5sc.png




Ok, so moving on to your latest example code that works on XP and W10 ... is "c:\test.xls" (and by the way, you really, really should try to avoid saving files into the root of your system drive under Windows 10) the same file as Text1.Text in the version that failed?
 
Is this the right one?

New_Bitmap_Image_vznvfm.jpg


No, "c:\test.xls" is just for this testing program; the real program is using another Excel files on different drives, not C:, and those files are changed every month so I cannot hard code those files.

This "c:\test.xls" was open using Excel 2007 when tested on Windows 10 and Excel 2000 on XP (XP has only one version of Excel, Windows 10 has both 2000 and 2007).
 
For late binding, if you compile and then run the .exe As Administrator (not running it through the VB environment), do you still get that same error?

What is an example of the value of Text1.Text when it fails?
 
Yes, still the same error even when run as Administrator.

A real 'Text1.text' value would be like 'G:\SVR-2\Lilly\Cuts\2020\J&G-Oct 2019.xls'

Thx
 
>This "c:\test.xls" was open using Excel 2007 when tested on Windows 10 and Excel 2000 on XP

Which is what we'd expect

>No, "c:\test.xls" is just for this testing program

Reason I ask is that sometimes this or a similar issue is caused by the file being corrupt (as far as Excel is concerned), or issues with permissions. If we target a different file for testing, then we prove little

One more thing - what happens if you try and open one of the real files directly from within Excel on W10?

>Is this the right one?

Nope. Sorry, I guess I wasn't clear.

So you find the PROGID for Excel under HKEY_CLASSES_ROOT (in my picture, this is Excel.Application.16, but you'll be looking for Excel.Application.9). You then want to note the CLSID value, as highlighted in yellow (you should find the same value as shown, even though looking at a different version of Excel)]

PROGID_gssjhn.png


It is this value that you then want to search for under HKEY_CLASSES_ROOT\WOW6432Node\CLSID

CLSID2_qce49m.png
 
> what happens if you try and open one of the real files directly from within Excel on W10?

It opens in both Excel version correctly.

Hope this is the right one:
123_uduigm.png
 
I assume your 'little' test app did not have a reference to Excel object.
What about your 'regular' app? Do you still have a reference to Excel?
If you do, I would remove it and try that. With late binding you do not need this reference.


---- Andy

There is a great need for a sarcasm font.
 
>Hope this is the right one

It is - but there's some MSI magic going on with your entry,which means you have what is known as a Darwin Descriptor, and it would take to much time to explain how and why that works ... fortunately the default entry contains enough info to confirm that, for you, the excel.9 CLSID leads to Excel.12
 
No, I don't have a reference to Excel in my 'regular app'

I tried to hard code file locations in my 'regular app' but it failed saying error in line 111 (see below)

Code:
111 Set wb1 = xlApp1.Workbooks.Open("G:\CUTS\2020\J&G - OCT 2019.xls")
 
I am sorry for making this threat endless, but I have a good news - major breakthrough LOL
I was sable to pass over that line giving me errors before by adding 'xlApp1.Visible = True'; it doesn't make much sense to me but it works.
Now I am getting "Error # 6, Error Line: 290, Overflow"

The current code
Code:
Private Sub Command3_Click()
      'COMPARE LUMBER INVENTORY
          Dim xlApp1 As Object
          Dim wb1 As Object
          Dim ws1 As Object
          Dim xlApp2 As Object
          Dim wb2 As Object
          Dim ws2 As Object
          Dim i, j As Long
          Dim Msg As String

10        On Error GoTo NAV1

20        If Len(Text1.Text) < 2 Then
30            MsgBox "Select first file!"
40            Exit Sub
50        End If

60        If Len(Text2.Text) < 2 Then
70            MsgBox "Select second file!"
80            Exit Sub
90        End If

100       Set xlApp1 = CreateObject("Excel.Application")
110       xlApp1.Visible = True
120       Set wb1 = xlApp1.Workbooks.Open(Text1.Text)
130       Set ws1 = wb1.Worksheets(1)

140       Set xlApp2 = CreateObject("Excel.Application")
150       xlApp2.Visible = True
160       Set wb2 = xlApp2.Workbooks.Open(Text2.Text)
170       Set ws2 = wb2.Worksheets(1)

180       Me.MousePointer = vbHourglass

190       For i = 1 To Text3.Text
200           Label1.Caption = i

210           If (ws2.Cells(i, 7) = "GRN" Or ws2.Cells(i, 7) = "KD" Or ws2.Cells(i, 7) = "KDOS" Or ws2.Cells(i, 7) = "HC" Or ws2.Cells(i, 7) = "FOHC") Then
220               For j = 1 To Text3.Text
230                   Label2.Caption = j
240                   Label2.Refresh
250                   If ws1.Cells(j, 1) = ws2.Cells(i, 1) Then
260                       If ws1.Cells(j, 8) = ws2.Cells(i, 8) Then

270                           ws2.Cells(i, 12) = ws1.Cells(j, 12)

280                       Else
290                           ws2.Cells(i, 12) = ws1.Cells(j, 12) / ws1.Cells(j, 8) * ws2.Cells(i, 8)
300                       End If
310                       Exit For    'new line
320                   End If
330               Next j
340           End If
350       Next i

360       Label2.Caption = ""
370       Label2.Refresh

380       Set ws1 = Nothing
390       wb1.Save
400       wb1.Close
410       Set wb1 = Nothing
420       Set xlApp1 = Nothing

430       Set ws2 = Nothing
440       wb2.Save
450       wb2.Close
460       Set wb2 = Nothing
470       Set xlApp2 = Nothing

480       Me.MousePointer = vbDefault

490       MsgBox "Done"
NAV1:
500       Msg = "Error # " & Str(Err.Number) & " was generated by " _
              & Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
510       MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
511 End Sub
 
If Error Line: 290 creates an Overflow error, that would mean that you exceeded the ability to hold a number for either i or j variables. Strange, because i is a Variant and j is a Long, so they can hold pretty large numbers…


---- Andy

There is a great need for a sarcasm font.
 
Yes, max value of both variables, i and j, is no less than 400 (number of lines).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top