Question : Why I can't open database again at Worksheet_Change event

Hi,
I have a question about Why I can't open database again at Worksheet_Change event
MsgBox rst.RecordCount , it return -1

anyone could help?
Thank you!
Francis  SZE
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
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

Answer : Why I can't open database again at Worksheet_Change event

sorry solved by
rst.ActiveConnection = cnn
rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
rst.LockType = adLockBatchOptimistic
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
Private Sub Worksheet_Activate()
Call sub1
End Sub

Private Sub sub1()
  
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim lst As String

Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset


cnn.Open "kennamora", "kennam", "kennam007"
rst.ActiveConnection = cnn
rst.CursorLocation = adUseClient
rst.CursorType = adOpenStatic
rst.LockType = adLockBatchOptimistic

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
cnn.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.CursorLocation = adUseClient
        rst.CursorType = adOpenStatic
        rst.LockType = adLockBatchOptimistic
        
        
        rst.Source = "SELECT * FROM customer_produces where com_id=" & spitted(1)
        rst.Open
        
        MsgBox rst.RecordCount
        
        
        
        rst.Close
        cnn.Close
        
        Set rst = Nothing
        Set cnn = Nothing


    End If
End Sub
Random Solutions  
 
programming4us programming4us