I am new to excel ..... well i have 2 excel sheets one as a database and another to display the result based on some selection
enter code here
//for database connectivity
Option Explicit
Public cnn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public strSQL As String
Public Sub OpenDB()
If cnn.State = adStateOpen Then cnn.Close
cnn.ConnectionString =
"Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
cnn.Open
End Sub
Public Sub closeRS()
If rs.State = adStateOpen Then rs.Close
rs.CursorLocation = adUseClient
End Sub
// code to display on another sheet
Private Sub cmdReset_Click()
'clear the data
ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear
Sheets("View").Visible = True
Sheets("View").Select
Range("dataSet").Select
Range(Selection, Selection.End(xlDown)).ClearContents
End Sub
Private Sub cmdShowData_Click()
'populate data
strSQL = "SELECT * FROM [data$] WHERE "
If ComboBox1.Text <> "" Then
strSQL = strSQL & " [Product]='" & ComboBox1.Text & "'"
End If
If ComboBox2.Text <> "" Then
If ComboBox1.Text <> "" Then
strSQL = strSQL & " AND [Region]='" & ComboBox2.Text & "'"
Else
strSQL = strSQL & " [Region]='" & ComboBox2.Text & "'"
End If
End If
If ComboBox3.Text <> "" Then
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Then
strSQL = strSQL & " AND [Customer Type]='" & ComboBox3.Text & "'"
Else
strSQL = strSQL & " [Customer Type]='" & ComboBox3.Text & "'"
End If
End If
If ComboBox1.Text <> "" Or ComboBox2.Text <> "" Or ComboBox3.Text <> "" Then
'now extract data
closeRS
OpenDB
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Sheets("View").Visible = True
Sheets("View").Select
Range("dataSet").Select
Range(Selection, Selection.End(xlDown)).ClearContents
'Now putting the data on the sheet
ActiveCell.CopyFromRecordset rs
Else
MsgBox"I was not able to find any matching records.",vbExclamation+ vbOKOnly
Exit Sub
End If
closeRS
OpenDB
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
Range("L6").CopyFromRecordset rs
Else
Range("L6:M7").Clear
MsgBox "There was some issue getting the totals.", vbExclamation + vbOKOnly
Exit Sub
End If
End If
End If
End Sub
Private Sub ComboBox1_DropButtonClick()
Dim v, e
With Sheets("data").Range("b2:b15")
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
End With
End Sub
Private Sub ComboBox2_DropButtonClick()
Dim v, e
With Sheets("data").Range("c2:c15")
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox2.List = Application.Transpose(.keys)
End With
End Sub
Private Sub ComboBox3_DropButtonClick()
Dim v, e
With Sheets("data").Range("d2:d15")
v = .Value
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox3.List = Application.Transpose(.keys)
End With
End Sub
and snapshot to get better view [displaying sheet][1] [1]: http://i.stack.imgur.com/pWBoJ.jpg [input sheet with hyperlinks][2] [2]: http://i.stack.imgur.com/lYF3K.jpg now the problem is I want to create a hyperlink on input sheet so that when the data is displayed in output sheet the hyperlink also displayed
also these 2 sheets are in same workbook Thanks everyone in advance