Question : Access Form using SQL to pull data from Terradata database

I have some SQL code which is run against an NCR Terradata database using NCR Queryman.  I was hoping to integrate this code into an access (97) database.

I am looking to have a simple access form where I have a field to input the value specified as "ROT.Address_Line4" and also another field for the variable specified as "BPR.Base_Product_Number = 57574598".

This would allow me to have an access form where a user would simply enter the address value and the product number and hopefully retrieve the relevant results without having manipulate the SQL.

Can anyone help as access and SQL are not my strong points.

The SQL I use in Queryman to pull the required data is:

SELECT      
CAL.Calendar_Date
,ROT.Retail_Outlet_Number
,BPR.Base_Product_Number
,Stock_Holding_Qty
,Stock_Holding_Wgt
,Stock_Count_Ind
FROM      VWI0DCL_STR_CLOSING_STK_ALL DCL
INNER JOIN VWI0ROT_RETAIL_OUTLET ROT
      ON      ROT.Retail_Outlet_Number = DCL.Retail_Outlet_Number
INNER JOIN VWI0CAL_SMALL_CALENDAR CAL
      ON      CAL.Calendar_Date = DCL.Calendar_Date
INNER JOIN VWI7BPR_BASE_PRODUCT_SNAPSHOT BPR
      ON      BPR.Base_Product_Number = DCL.Base_Product_Number
WHERE      CAL.Calendar_Date = DATE -1
      AND      ROT.Address_Line4 = 'LOTHIAN'
      AND      BPR.Base_Product_Number = 57574598

Answer : Access Form using SQL to pull data from Terradata database

Sorry I haven't gotten back to you, been a bit busy.

I actually do a lot of Access/Teradata interaction - unfortunately, the only way for you to run this sort of dynamic query is to do it all in VBA.  (As far as I know).  Pass-through queries are great as long as you don't have variables you want to work with.

For example, here's some of the VBA I use to run a query and get a recordset returned.  As you can see, it's pretty long, though there's a lot of unnecessary stuff in there.  Once you have a recordset, you can export that however you want.  Let me know where you get stuck.

Public Sub RunPassThroughQuery(StartDate As Date, EndDate As Date, Username As String, Password As String)

    Dim PassThroughSQL As String
    Dim FixedStartDate As String
    Dim FixedEndDate As String
    Dim oConn As ADODB.Connection
    Dim oRec As ADODB.Recordset
    Dim conStr As String   'connection string
    Dim rConn As ADODB.Connection
    Dim strDir As String
   
    On Error GoTo ErrorHandle
   
    'setup rConn (resultset connection)
    Set rConn = CurrentProject.Connection
   
    'setup oConn (opened connection) and recordset
    'cursorlocation pulls all of the data to the client instead of running
    'a cursort from the database server
   
    Set oConn = New ADODB.Connection
    Set oRec = New ADODB.Recordset
    oConn.CursorLocation = adUseClient
   
    'open database connection
    oConn.Open "Driver={Teradata};DBCName=[my DBCName];Database=[My Database];Connect Timeout=1000;Uid=" & Username & ";Pwd=" & Password
    'set this high so the query won't timeout
    oConn.CommandTimeout = 1000

    'fix the dates to Teradata format
    If Len(DatePart("m", StartDate)) = 1 And Len(DatePart("d", StartDate)) = 1 Then
        FixedStartDate = DatePart("yyyy", StartDate) & "-0" & DatePart("m", StartDate) & "-0" & DatePart("d", StartDate)
    ElseIf Len(DatePart("m", StartDate)) = 1 And Len(DatePart("d", StartDate)) <> 1 Then
        FixedStartDate = DatePart("yyyy", StartDate) & "-0" & DatePart("m", StartDate) & "-" & DatePart("d", StartDate)
    ElseIf Len(DatePart("m", StartDate)) <> 1 And Len(DatePart("d", StartDate)) = 1 Then
        FixedStartDate = DatePart("yyyy", StartDate) & "-" & DatePart("m", StartDate) & "-0" & DatePart("d", StartDate)
    Else
        FixedStartDate = DatePart("yyyy", StartDate) & "-" & DatePart("m", StartDate) & "-" & DatePart("d", StartDate)
    End If
   
    If Len(DatePart("m", EndDate)) = 1 And Len(DatePart("d", EndDate)) = 1 Then
        FixedEndDate = DatePart("yyyy", EndDate) & "-0" & DatePart("m", EndDate) & "-0" & DatePart("d", EndDate)
    ElseIf Len(DatePart("m", EndDate)) = 1 And Len(DatePart("d", EndDate)) <> 1 Then
        FixedEndDate = DatePart("yyyy", EndDate) & "-0" & DatePart("m", EndDate) & "-" & DatePart("d", EndDate)
    ElseIf Len(DatePart("m", EndDate)) <> 1 And Len(DatePart("d", EndDate)) = 1 Then
        FixedEndDate = DatePart("yyyy", EndDate) & "-" & DatePart("m", EndDate) & "-0" & DatePart("d", EndDate)
    Else
        FixedEndDate = DatePart("yyyy", EndDate) & "-" & DatePart("m", EndDate) & "-" & DatePart("d", EndDate)
    End If
   
    'Here's the large SQL query [it's really long in reality]
      PassThroughSQL = "select * from usertable"
   
    'debugging - outputs query to file
    'fhandle = FreeFile(0)
    'strDir = "h:\projects\access\contracts\query.txt"

    'Open strDir For Output As #fhandle
    'Print #fhandle, PassThroughSQL
    'Close #fhandle
    'Exit Sub
   
    'Run the SQL command on the Teradata warehouse
    oRec.Open PassThroughSQL, oConn 'set other attributes if necessary
   
    'Make sure there are records to pull - if not there's some
    'sort of development/recordset problem
    If oRec.RecordCount < 1 Then
        MsgBox "There were no records to import.  This is probably a bug, please contact development."
        Exit Sub
    End If

      ' this is the section where I do a row-by-row insert of the recordset into my database.  
      ' this is where you would do your output to text, screen, or Excel since you're not storing it in a table
   
    'cleanup
    oRec.Close
    oConn.Close
    rConn.Close
   
    Exit Sub
   
ErrorHandle:

MsgBox Error()
Exit Sub

End Sub


Random Solutions  
 
programming4us programming4us