Hi Everyone,
Have written code to pull data from one excel sheet to another using SQL VBA Query. The user interface has multiple filters (combo box) to filter data from the source excel sheet (serving as database). UI and database sheet are in same workbook.Everything thing is working fine apart from one thing . The data that is getting pulled/filtered is getting pasted as values/text format, The need is that the data should get filtered in the same format as Source sheet (Database).
Here is the piece of code that is written :
Kindly suggest me how I Can pull/filter data in same format as of my source excel sheet.
With Regards,
Gauraw
Have written code to pull data from one excel sheet to another using SQL VBA Query. The user interface has multiple filters (combo box) to filter data from the source excel sheet (serving as database). UI and database sheet are in same workbook.Everything thing is working fine apart from one thing . The data that is getting pulled/filtered is getting pasted as values/text format, The need is that the data should get filtered in the same format as Source sheet (Database).
Here is the piece of code that is written :
Code:
Private Sub cmdShowData_Click()
Application.ScreenUpdating = False
'populate data
strSQL = "SELECT [Tilte],[Body],[Source],[Published Date] FROM [Database$] WHERE "
If ComboBox1.Text <> "" Then
strSQL = strSQL & " [Geography]='" & ComboBox1.Text & "'"
End If
If ComboBox2.Text <> "" Then
If ComboBox1.Text <> "" Then
strSQL = strSQL & " AND [Therapy Area]='" & ComboBox2.Text & "'"
Else
strSQL = strSQL & " [Therapy Area]='" & ComboBox2.Text & "'"
End If
End If
If ComboBox3.Text <> "" Then
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Then
strSQL = strSQL & " AND [Indication]='" & ComboBox3.Text & "'"
Else
strSQL = strSQL & " [Indication]='" & ComboBox3.Text & "'"
End If
End If
If ComboBox4.Text <> "" Then
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Or ComboBox3.Text <> "" Then
strSQL = strSQL & " AND [Company]='" & ComboBox4.Text & "'"
Else
strSQL = strSQL & " [Company]='" & ComboBox4.Text & "'"
End If
End If
If ComboBox5.Text <> "" Then
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Or ComboBox3.Text <> "" Or ComboBox4.Text <> "" Then
strSQL = strSQL & " AND [News Category]='" & ComboBox5.Text & "'"
Else
strSQL = strSQL & " [News Category]='" & ComboBox5.Text & "'"
End If
End If
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Or ComboBox3.Text <> "" Or ComboBox4.Text <> "" Or ComboBox5.Text <> "" Then
'now extract data
closeRS
OpenDB
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Sheets("UI").Visible = True
Sheets("UI").Select
Range("dataSet").Select
Range(Selection, Selection.End(xlDown)).ClearContents
'Now putting the data on the sheet
ActiveCell.CopyFromRecordset rs
Else
MsgBox "No Matching Recoreds Found!.", vbExclamation + vbOKOnly
Exit Sub
End If
End If
Application.ScreenUpdating = True
End Sub
Kindly suggest me how I Can pull/filter data in same format as of my source excel sheet.
With Regards,
Gauraw