Results 1 to 17 of 17

Thread: Excel and ADO

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Excel and ADO

    Anyone ever experience this with an Update Statement

    I keep getting this: "syntax error (missing operator) in query expression"

    Code:
        Dim adoConn As New ADODB.Connection
        Dim adoRS As New ADODB.Recordset
        adoConn.CommandTimeout = 3
        adoConn.ConnectionTimeout = 3
        adoConn.CursorLocation = adUseClient
        adoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Chr(34) & gstrResultFile & Chr(34) & ";Persist Security Info=False;Extended Properties=" & Chr(34) & "Excel 8.0;HDR=Yes" & Chr(34)
    
    'This works fine
        'pi_strSQL = "UPDATE [Path1$] SET [Path1$].[Processed]=1 WHERE [Path1$].[Processed]=0"   
    
    'This does not work - the only difference is the 'FROM [Path1$]'
        pi_strSQL = "UPDATE [Path1$] SET [Path1$].[Processed]=1 FROM [Path1$] WHERE [Path1$].[Processed]=0"   
    
        Call adoConn.Execute(pi_strSQL)
        
        adoRS.Close
        Set adoRS = Nothing
        adoConn.Close
        Set adoConn = Nothing
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    The FROM is invalid in that case, what are you trying to do differently from the working version?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    Quote Originally Posted by si_the_geek
    The FROM is invalid in that case, what are you trying to do differently from the working version?
    How would I do an inner join with two tables for an UPDATE statement?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Excel and ADO

    Quote Originally Posted by Liquid Metal
    Code:
        pi_strSQL = "UPDATE [Path1$]  SET [Path1$].[Processed]=1 FROM [Path1$] WHERE [Path1$].[Processed]=0"
    If you want to update your table from another table, you could do something like this:
    This Updates [Path1$] from [Path2$], works fine in Access, I don't know if this works with Excel.
    Code:
    UPDATE [Path1$] 
         SET [Path1$].[Processed] = 1 
     WHERE [Path1$].[Processed] IN 
           (Select T.[Processed] 
             FROM [Path2$] T 
           WHERE T.[Processed] = 0);

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    That works JCIS! But it is kind of strange it does not take inner joins, rather it takes correlated/sub queries. How would I do an inner join than?

    Code:
    '---------------------------------------------------------------------------
    Dim strSQL As String
    strSQL = ""
    strSQL = strSQL & "UPDATE [Path1$]" & vbCrLf
    strSQL = strSQL & "SET [Path1$].[Processed] = 1 " & vbCrLf
    strSQL = strSQL & "WHERE EXISTS (SELECT * FROM [Path2$]" & vbCrLf
    strSQL = strSQL & "WHERE [File]=[Path1$].[File] AND [XPath]=[Path1$].[XPath])" & vbCrLf
    strSQL = strSQL & "AND [Path1$].[Processed] = 0" & vbCrLf
    '---------------------------------------------------------------------------
    What I really need is to update the Processed field for 2 tables at once. Is that possible?

    Thank You
    Last edited by Liquid Metal; Jan 25th, 2006 at 07:45 PM.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    I am in still interest in wanting to know why this work in Query Anaylzer but does not work through the above code.

    Code:
    UPDATE dbo.Table_2 SET
    dbo.Table_2.Data = 'Frack'
    FROM 
    	dbo.Table_1 T1
    INNER JOIN
    	dbo.Table_2 T2
    ON T1.Buffer = T2.Buffer
    
    SELECT * FROM dbo.Table_2
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    That will work thru code... for SQL Server databases.

    Unfortunately SQL varies by DBMS and/or driver (even tho it is theoretically standardised), and Update is one situation where the difference shows more than most.

    What I really need is to update the Processed field for 2 tables at once. Is that possible?
    Not in a single update statement I'm afraid. If you use SQL Server (or other major DBMS) you can create a stored procedure, otherwise the best you can do is multiple Updates within a transaction.

  8. #8
    Junior Member
    Join Date
    Nov 2005
    Location
    NE Kansas
    Posts
    29

    Re: Excel and ADO

    Here is a suggestion for troubleshooting embedded sQL statements.

    Capture the EXACT SQL statement using an error handler that pastes the SQL into a text box. Then you can copy the SQL and paste it into an SQL tool. It really makes like a lot easier, because the SQL errors that VB displays are often not what is acutally happening.

    Build a form, frmErrorDisplay, that has the necessary objects to accept the passed strings.

    For example:

    VB Code:
    1. Private Sub cmbLogIn_Click()
    2.   On Error GoTo ErrorRoutineErr
    3.  
    4.   .....
    5.   .....
    6.  
    7.   ' Capture SQL string for error trap
    8.   SQLHoldString = strSql
    9.  
    10.   rstUser.Open strSql, cnn, , , adCmdText
    11.   SQLHoldString = ""
    12.  
    13.   .....
    14.   .....
    15.  
    16. ErrorRoutineResume:
    17.   Exit Sub
    18. ErrorRoutineErr:
    19.     If Not SQLHoldString = "" Then
    20.     Beep
    21.     frmErrorDisplay.txInfoWindow.Text = SQLHoldString
    22.     frmErrorDisplay.lbSQLDisplay.Caption = SQLHoldString
    23.     frmErrorDisplay.lbSQLErrorMsg.Caption = "SQL Error --> " & Err & " - " & Error
    24.     frmErrorDisplay.lbErrorMessage.Caption = "Error attempting to Login !" + CRLF + _
    25.                     "There may be SQL Syntax error(s)." + CRLF + _
    26.                     "Contact the system developer for assistance."
    27.     Set gCallingForm = Me
    28.     frmLogIn.Enabled = False
    29.     frmErrorDisplay.Show vbModal
    30.     frmLogIn.Enabled = True
    31.     Exit Sub
    32.   End If
    33.   gIsError = True
    34.   MsgBox "Login Module  --> " & Err & " - " & Error
    35.   Resume Next
    36.  
    37. End Sub

    Hope this helps.

    K Hoar, President
    BigDog Technologies

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    Very true, this FAQ thread has similar suggestions.

  10. #10
    Junior Member
    Join Date
    Nov 2005
    Location
    NE Kansas
    Posts
    29

    Re: Excel and ADO

    Debug.Print is sometimes okay, but only when developing the application and for very simple SQL statements.

    Many times the SQL statement can be way, way too long for the immediate window. I have several apps where some of the SQL statements are 50 lines or more long.

    Using the error form, if a client has a problem, I can walk him through copying the SQL statement and then he can paste it into an email and send it to me.

    Makes supportung multiple, remote applications much easier.

    K Hoar, President
    BigDog Technologies

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    True, which is one reason I made the SQL formatter (link via the FAQ thread), so that long SQL statements are much easier to read.

    I personally don't like the idea of letting users see the SQL, it's a bit of a risk in terms of security etc (they may try running their own version). In situations like that I prefer to save the SQL statement to a file (with simple encryption), which can be emailed if appropriate.

    If you have access to the database, you can save the SQL to an error log table, or for larger DBMS's (such as SQL Server/Oracle) you can check error logs etc.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    Thank you for your replies guys.

    So, how do I update an Excel Spreadsheet using ADO?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  13. #13
    Junior Member
    Join Date
    Nov 2005
    Location
    NE Kansas
    Posts
    29

    Re: Excel and ADO

    I would not know as I would never consider using ADO to write to Excel. Excel is not a database.

    If I were foing to write to a spreadsheet such as Excel, I would open an instance of Excel, such as:

    VB Code:
    1. Set vbExcel = CreateObject("Excel.Application")
    2.  
    3.   ' For TESTING ONLY !!!!!  If you want Excel to be visible
    4.   If gRunMode = "Testing" Then
    5.     vbExcel.Visible = True
    6.   End If

    Then I would write to the spreadsheet using VBA code:

    VB Code:
    1. ' Capture Raw Data, if flag is set
    2.   If gCaptureRawData = "YES" Then
    3.  
    4.     ' Activate data tab
    5.     vbWorkbook.Sheets("Data").Select
    6.  
    7.     ' Find last row
    8.     LastRow = MaxRow("B4")
    9.  
    10.     ' Delete current data, if any exists
    11.     If LastRow > 3 Then
    12.       Range("B4:S" + RTrim(CStr(LastRow))).Select
    13.       Selection.ClearContents
    14.     End If
    15.     Range("B4").Select
    16.  
    17.     ' Write result set to Excel data worksheet
    18.     RowCounter = 4
    19.     rstDetail.MoveFirst
    20.     Do Until rstDetail.EOF
    21.       Range("B" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!AgencyName)
    22.       Range("C" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!Address)
    23.       Range("D" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!CityState)
    24.       Range("E" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!SubAgent)
    25.       Range("F" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!Underwriter)
    26.       Range("G" + RTrim(CStr(RowCounter))).Value = rstDetail!ClientNumber
    27.       Range("H" + RTrim(CStr(RowCounter))).Value = rstDetail!CustomerState
    28.       Range("I" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!CustomerName)
    29.       Range("J" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!PolicyDesc)
    30.       Range("K" + RTrim(CStr(RowCounter))).Value = RTrim(rstDetail!SpecProv)
    31.       Range("L" + RTrim(CStr(RowCounter))).Value = rstDetail!OrderNo
    32.       Range("M" + RTrim(CStr(RowCounter))).Value = CDbl(rstDetail!PremiumDue)
    33.       Range("N" + RTrim(CStr(RowCounter))).Value = CDbl(rstDetail!PremiumPaid)
    34.       Range("O" + RTrim(CStr(RowCounter))).Value = CDbl(rstDetail!NetDueMMC)
    35.       Range("P" + RTrim(CStr(RowCounter))).Value = CDbl(rstDetail!Commission)
    36.       Range("Q" + RTrim(CStr(RowCounter))).Value = rstDetail!RA_LName
    37.       Range("R" + RTrim(CStr(RowCounter))).Value = CDbl(rstDetail!RA_FlatFee)
    38.         Range("S" + RTrim(CStr(RowCounter))).Value = rstDetail!VoidIndicator
    39.       rstDetail.MoveNext
    40.       RowCounter = RowCounter + 1
    41.     Loop
    42.   End If

    K Hoar, President
    BigDog Technologies

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    Quote Originally Posted by Liquid Metal
    Thank you for your replies guys.

    So, how do I update an Excel Spreadsheet using ADO?
    I was under the impression you had that part working

    What is the current situation?

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    Thank You si_the_geek!

    It was the same old stuff. I couldn't update on a join but when I take the join out, it works fine.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Excel and ADO

    Thanks Hoar.

    I understand your reference about not using Excel as a database. This was a special case.

    Thank You
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel and ADO

    I'm afraid it look as if jcis's method (post #4) is about all you can do with Excel databases.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width