Advertisement

04.02.2004 at 10:47AM PST, ID: 20941749
[x]
Attachment Details

SQL - ODBC - VBA in Excel - Run-time error '1004'

Asked by Marc-Olivier in Microsoft Excel Spreadsheet Software

Tags: odbc, error, 1004, excel, vba

Hi,

I built a code that executes an SQL code over an ODCB connection.  My problem is that this code is working well on my computer but isn't working on 6 others similar computer that I tried my code.

Error message : Run-Time Error '1004'; Application-defined or object-defined error.  This appear at the last line of the code: ThisQt.Refresh.

Here is my code :
Sub Import()
Dim thisSheet As Worksheet
Dim sql As String
Dim sqlWhere As String
Dim DB_NAME As String
Dim connString As String
Dim thisQT As QueryTable
Dim StartDate As String
Dim EndDate As String

Sheet1.Range("A3:AV65536").Clear

Set thisSheet = Sheet1

StartDate = Year(Sheet2.Range("B5")) & Format(Month(Sheet2.Range("B5")), "00") & Format(Day(Sheet2.Range("B5")), "00")
EndDate = Year(Sheet2.Range("b6")) & Format(Month(Sheet2.Range("b6")), "00") & Format(Day(Sheet2.Range("b6")), "00")

sql = "SELECT * FROM CANADA.KB4400CSTM.FCMSFC54C FCMSFC54C"

If Sheet2.Range("B3") = "" Then
    If Sheet2.Range("B4") = "" Then
        sqlWhere = " WHERE (FCMSFC54C.L5DTTS between " & StartDate & " And " & EndDate & ")"
        Else
        sqlWhere = " WHERE (FCMSFC54C.L5CO=" & Sheet2.Range("B4") & ") AND (FCMSFC54C.L5DTTS between " & StartDate & " And " & EndDate & ")"
    End If
    Else
    If Sheet2.Range("B4") = "" Then
        sqlWhere = " WHERE (FCMSFC54C.L5DPTG='" & Sheet2.Range("B3") & "') AND (FCMSFC54C.L5DTTS between " & StartDate & " And " & EndDate & ")"
        Else
        sqlWhere = " WHERE ((FCMSFC54C.L5CO=" & Sheet2.Range("B4") & ") AND (FCMSFC54C.L5DPTG='" & Sheet2.Range("B3") & "') AND (FCMSFC54C.L5DTTS between " & StartDate & " And " & EndDate & "))"
    End If
End If

DB_NAME = "AS400"

connString = "ODBC;DRIVER={Client Access ODBC Driver (32-bit)};SYSTEM=CANADA;DBQ=AS400"

Set thisQT = thisSheet.QueryTables.Add(connString, Sheet1.Range("A3"), sql & sqlWhere)

thisQT.BackgroundQuery = False
thisQT.Refresh

End Sub

Many thanks

MarcStart Free Trial
[+][-]04.02.2004 at 11:17AM PST, ID: 10743569

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2004 at 11:50AM PST, ID: 10743857

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.03.2004 at 02:40AM PST, ID: 10747096

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.05.2004 at 07:53AM PDT, ID: 10757797

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.06.2004 at 01:13PM PDT, ID: 10769351

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Excel Spreadsheet Software
Tags: odbc, error, 1004, excel, vba
Sign Up Now!
Solution Provided By: smozgur
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.06.2004 at 01:18PM PDT, ID: 10769399

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2004 at 01:59PM PDT, ID: 10845932

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.19.2004 at 06:09AM PDT, ID: 10859133

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.19.2004 at 06:10AM PDT, ID: 10859148

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32