Need to paste result output of two queries with both headers in same page!!

saranraj

New Member
Joined
Apr 6, 2016
Messages
1
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
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top