Private Sub Worksheet_Activate()
Call sub1
End Sub
Private Sub sub1()
Dim wb As Workbook
Dim ws As Worksheet, ws2 As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim lst As String
Dim i As Long
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open "kennamora", "kennam", "kennam007"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseServer
rst.Source = "SELECT * FROM customers "
rst.Open
While Not rst.EOF
lst = lst & rst.Fields("com_address").Value & ":" & rst.Fields("tid").Value & ","
rst.MoveNext
Wend
rst.Close
Set rst = Nothing
Set cnn = Nothing
On Error GoTo n1
With ws.Range("F9").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="" & lst
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = "Please select from dropdown list."
.ShowInput = True
.ShowError = True
End With
Exit Sub
n1:
MsgBox Err.Description
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$9" Then
' MsgBox Range("F9").Value
spitted = Split(Range("F9").Value, ":")
' MsgBox spitted(1)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset
cnn.Open "kennamora", "kennam", "kennam007"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseServer
rst.Source = "SELECT * FROM customer_produces where com_id=" & spitted(1)
rst.Open
MsgBox rst.RecordCount
rst.Close
Set rst = Nothing
Set cnn = Nothing
End If
End Sub
|