Hello All
I need to paste the output of two queries in same worksheet. When my code is executed it pastes the header and values of first query. But for the second query i get only values and not header.. Please help me.. Below is my code..
Private Sub CommandButton1_Click()
Label5 = " "
''Taking values from text fields''
dbname = ""
strId = ""
pass = ""
''Cheking the empty fields''
If pass = "" Or strId = "" Or dbname = "" Then
Label5 = "Enter all Fields!!!"
GoTo JMP
End If
''Checking the check boxes
If Sheets("IDCARD_MEDICAL").CheckBox1 = "False" And Sheets("IDCARD_MEDICAL").CheckBox2 = "False" Then
Label5 = "Select atleast one check box!!!"
GoTo JMP
End If
With Sheets("GrandFather")
.Visible = True
.Activate
Range("A1:XFD1048576").Clear
End With
With Sheets("Medicap")
.Visible = True
.Activate
Range("A1:XFD1048576").Clear
End With
''queries''
''Creating connection to Database''
Label5 = "Connecting..."
Dim cnConn As Connection
Set cnConn = New Connection
cnConn.ConnectionString = "Provider=MSDAORA.1;" & "Password=" & pass & "; User ID=" & strId & ";Data Source=" & dbname & "; Persist Security Info=True"
cnConn.CursorLocation = adUseClient
cnConn.Open
''for grandfather group
If Sheets("IDCARD_MEDICAL").CheckBox1.Value = True Then
Dim col As Integer
Dim row As Integer
Dim query As String
Dim mtxData As Variant
Dim rsTemp As Recordset
Set rsTemp = New Recordset
query1 = _
"select DER,FRE,TRE from CSDEL where FRE ='" & Sheets("IDCARD_MEDICAL").Range("F27").Value & "' and DER='" & Sheets("IDCARD_MEDICAL").Range("G27").Value & "'"
rsTemp.Open query1, cnConn, adOpenStatic, adLockReadOnly
With Sheets("GrandFather")
col = 0
'First Row: names of columns
Do While col < rsTemp.Fields.Count
.Cells(1, col + 1) = rsTemp.Fields(col).Name
col = col + 1
Loop
Set ws = ActiveWorkbook.Sheets("GrandFather")
Set r = ws.Range("A2")
r.CopyFromRecordset rsTemp
rsTemp.Close
End With
'' --------***** This result header is not populated ******----------
query1 = _
"select DER,FRE,TRE from CSDEL where FRE ='" & Sheets("IDCARD_MEDICAL").Range("F27").Value & "' and DER='" & Sheets("IDCARD_MEDICAL").Range("G27").Value & "'"
rsTemp.Open query1, cnConn, adOpenStatic, adLockReadOnly
With Sheets("GrandFather")
col = 0
'First Row: names of columns
Do While col < rsTemp.Fields.Count
.Cells(1, col + 1) = rsTemp.Fields(col).Name
col = col + 1
Loop
Set ws = ActiveWorkbook.Sheets("GrandFather")
Set r = ws.Range("A6")
r.CopyFromRecordset rsTemp
rsTemp.Close
End With
End If
cnConn.Close
Label5 = "Done."
JMP:
End Sub
Private Sub Label1_Click()
End Sub
Private Sub UserForm_Click()
End Sub
I need to paste the output of two queries in same worksheet. When my code is executed it pastes the header and values of first query. But for the second query i get only values and not header.. Please help me.. Below is my code..
Private Sub CommandButton1_Click()
Label5 = " "
''Taking values from text fields''
dbname = ""
strId = ""
pass = ""
''Cheking the empty fields''
If pass = "" Or strId = "" Or dbname = "" Then
Label5 = "Enter all Fields!!!"
GoTo JMP
End If
''Checking the check boxes
If Sheets("IDCARD_MEDICAL").CheckBox1 = "False" And Sheets("IDCARD_MEDICAL").CheckBox2 = "False" Then
Label5 = "Select atleast one check box!!!"
GoTo JMP
End If
With Sheets("GrandFather")
.Visible = True
.Activate
Range("A1:XFD1048576").Clear
End With
With Sheets("Medicap")
.Visible = True
.Activate
Range("A1:XFD1048576").Clear
End With
''queries''
''Creating connection to Database''
Label5 = "Connecting..."
Dim cnConn As Connection
Set cnConn = New Connection
cnConn.ConnectionString = "Provider=MSDAORA.1;" & "Password=" & pass & "; User ID=" & strId & ";Data Source=" & dbname & "; Persist Security Info=True"
cnConn.CursorLocation = adUseClient
cnConn.Open
''for grandfather group
If Sheets("IDCARD_MEDICAL").CheckBox1.Value = True Then
Dim col As Integer
Dim row As Integer
Dim query As String
Dim mtxData As Variant
Dim rsTemp As Recordset
Set rsTemp = New Recordset
query1 = _
"select DER,FRE,TRE from CSDEL where FRE ='" & Sheets("IDCARD_MEDICAL").Range("F27").Value & "' and DER='" & Sheets("IDCARD_MEDICAL").Range("G27").Value & "'"
rsTemp.Open query1, cnConn, adOpenStatic, adLockReadOnly
With Sheets("GrandFather")
col = 0
'First Row: names of columns
Do While col < rsTemp.Fields.Count
.Cells(1, col + 1) = rsTemp.Fields(col).Name
col = col + 1
Loop
Set ws = ActiveWorkbook.Sheets("GrandFather")
Set r = ws.Range("A2")
r.CopyFromRecordset rsTemp
rsTemp.Close
End With
'' --------***** This result header is not populated ******----------
query1 = _
"select DER,FRE,TRE from CSDEL where FRE ='" & Sheets("IDCARD_MEDICAL").Range("F27").Value & "' and DER='" & Sheets("IDCARD_MEDICAL").Range("G27").Value & "'"
rsTemp.Open query1, cnConn, adOpenStatic, adLockReadOnly
With Sheets("GrandFather")
col = 0
'First Row: names of columns
Do While col < rsTemp.Fields.Count
.Cells(1, col + 1) = rsTemp.Fields(col).Name
col = col + 1
Loop
Set ws = ActiveWorkbook.Sheets("GrandFather")
Set r = ws.Range("A6")
r.CopyFromRecordset rsTemp
rsTemp.Close
End With
End If
cnConn.Close
Label5 = "Done."
JMP:
End Sub
Private Sub Label1_Click()
End Sub
Private Sub UserForm_Click()
End Sub