i a have taken textbox(batchname) and button(download) please help me out..................
I am trying to download data using its batchname
prdTblName = "tblProd_" & proj_code & "_" & batch_Code
\ / /----
have tblprod_agr_006 many more
Dim cnt As ADODB.Connection
Dim rst1 As ADODB.Recordset, rst2 As ADODB.Recordset
Dim stDB As String, stSQL1 As String, stSQL2 As String
Dim strConn As String
Dim wbBook As Workbook
Dim Sheet1 As Worksheet
Dim dbconn As New ADODB.Connection
Dim prdTblName As String 'As ADODB.Connection
prdTblName = "tblProd_" & proj_code & "_" & batch_Code
Dim lnField As Long, lnCount As Long
Dim dataStr As String
'Instantiate the ADO-objects.
Set cnt = New ADODB.Connection
Set rst1 = New ADODB.Recordset
Set rst2 = New ADODB.Recordset
Set dbconn = New ADODB.Connection
Set wbBook = ThisWorkbook
Set Sheet1 = wbBook.Worksheets(1)
'Path to the database.
stDB = "mysql32"
'Create the connectionstring.
strConn = "Driver=MySQL ODBC 5.2 Unicode Driver;" _
& "Data Source=" & stDB & ";"
strSQL2 = "SELECT * FROM tblbatch_headers where idBatch " & batchID & "order by col_seq asc"
' Dim rst2 As New ADODB.Recordset
dbconn.Open strConn
rst2.Open strSQL2, dbconn, 3, 1
i = 1
Do While rst2.EOF = False
i = i + 1
If i = 1 Then
dataStr = rst2("tblColName")
Else
dataStr = dataStr & ", " & rst2("tblColName")
End If
rst2.MoveNext
Loop
'The 1st raw SQL-statement to be executed.
' stSQL1 = "SELECT * FROM tblbatch_headers where idBatch " & batchID & "order by col_seq asc"
'The 2nd raw SQL-statement to be executed.
stSQL2 = "SELECT * FROM " & prdTblName & " where FQR_User_Code='" & Application.username & _
"' and line_status in('QP') order by line_status"
'Clear the worksheet.
Sheet1.Range("A1:FA1").CurrentRegion.Clear
With cnt
.Open (strConn) 'Open the connection.
.CursorLocation = adUseClient 'Necessary to disconnect the recordset.
End With
' With rst1
' .Open stSQL1, cnt 'Create the recordset.
' Set .ActiveConnection = Nothing 'Disconnect the recordset.
'End With
With rst2
.Open stSQL2, cnt 'Create the recordset.
Set .ActiveConnection = Nothing 'Disconnect the recordset.
End With
With Sheet1
' .Cells(2, 1).CopyFromRecordset rst1 'Copy the 1st recordset.
.Cells(3, 1).CopyFromRecordset rst2 'Copy the 2nd recordset.
End With
'Release objects from the memory.
' rst1.Close
' Set rst1 = Nothing
rst2.Close
Set rst2 = Nothing
cnt.Close
Set cnt = Nothing
Bookmarks