Share via

Variable (inputbox) to be assigned in select CommandText -VBA

Anonymous
2013-06-13T19:53:22+00:00

Hi 

i am refining a recored macro , that pulls data from FoxPro to excel.code is correct i just want to add inputbox so as to get desire data i added highlighted command ,but if i write  **WHERE (HS_FILE.TR_TYPE='ttype')**variable****with single comma no data is extracted & if i write  **WHERE (HS_FILE.TR_TYPE=ttype)**without comma,****it shows "General ODBC error".

Thanks

Sub db_fox()

   Dim zCode As String

Dim tType As String


zCode = InputBox("Enter Zone Code :")

tType = InputBox("Enter Transaction Type :")

   With ActiveSheet.ListObjects.Add(

        "ODBC;CollatingSequence=ASCII;

        ), Array( _

        "L=dBase 5.0;MaxBufferSize=2048;

        )), Destination:=Range("$A$1")).

        .CommandText = Array( _

        "SELECT HS_FILE.TR_TYPE, HS_FILE.RTE_CODE, HS_FILE.ZNE_CODE, HS_FILE.PRD_CODE, HS_FILE.TR_QTY" & Chr(13) & "" & Chr(10) & "FROM HS_FILE HS_FILE" & Chr(13) & "" & Chr(10) & "WHERE (HS_FILE.TR_TYPE=ttype) AND (HS_FILE.ZNE_CODE=zcode)" _

        )

        .RowNumbers = False

        .FillAdjacentFormulas = False

        .PreserveFormatting = True

        .RefreshOnFileOpen = False

        .BackgroundQuery = True

        .RefreshStyle = xlInsertDeleteCells

        .SavePassword = False

        .SaveData = True

        .AdjustColumnWidth = True

        .RefreshPeriod = 0

        .PreserveColumnInfo = True

        .ListObject.DisplayName = "Table_Query_from_dpg"

        .Refresh BackgroundQuery:=False

    End With

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-06-13T22:28:32+00:00

The problem is that the query you are creating contains the literal string tttype which means nothing to the database.  It is a variable local to your VBA procedure and you want the value of that variable to be part of the string.  You do that by concatenating the variable with the rest of the string, and surrounding it with ' ' for the sake of the SQL syntax so:

"WHERE (HS_FILE.TR_TYPE='" & ttype & "') AND (HS_FILE.ZNE_CODE='" & zcode & "')"

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-06-14T20:53:29+00:00

    Perfect ! thanks again ...

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-06-13T23:47:26+00:00

    Thanks sir, i 'll try it

    Was this answer helpful?

    0 comments No comments