|
-
Feb 9th, 2011, 02:46 AM
#1
Thread Starter
Lively Member
connect to database Module ...Problem
Hi All
In the module i write this
Code:
Public Sub test()
Dim con1 As New adodb.Connection
con1.CursorLocation = adUseClient
con1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=test"
con1.Open
Dim k As New adodb.Recordset
Set k = New adodb.Recordset
k.Open "select * from table3", con1, adOpenDynamic, adLockOptimistic
Set Form1.DataGrid1.DataSource = k
End Sub
Public Sub count()
Dim con1 As New adodb.Connection
con1.CursorLocation = adUseClient
con1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=test"
con1.Open
Dim k As New adodb.Recordset
Set k = New adodb.Recordset
k.Open "select * from table3", con1, adOpenDynamic, adLockOptimistic
MsgBox k.RecordCount
End Sub
in the form1
Code:
Private Sub Form_Load()
test
End Sub
Private Sub Command1_Click()
count
End Sub
the form_load work and the module fill the datagrid1 with the data
but when i click command1 to count the record i receive this error
"Compile Error: Function or Interface Marked as Restricted, or the Function
Uses an Automation Type Not Supported in Visual Basic"
when i move the code in count() to form1.. it work
please help me out in this problem
regards for all
-
Feb 9th, 2011, 02:57 AM
#2
Re: connect to database Module ...Problem
In the public sub count, try removing the first four lines.
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Feb 9th, 2011, 03:11 AM
#3
Thread Starter
Lively Member
Re: connect to database Module ...Problem
still same problem .. i remove :
Dim con1 As New adodb.Connection
con1.CursorLocation = adUseClient
con1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=test"
con1.Open
?? & thanks in advance
-
Feb 9th, 2011, 03:44 AM
#4
Re: connect to database Module ...Problem
Hmm...which code does it highlight when the error exists?..
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Feb 9th, 2011, 04:19 AM
#5
Thread Starter
Lively Member
Re: connect to database Module ...Problem
-
Feb 9th, 2011, 04:43 AM
#6
Re: connect to database Module ...Problem
*ahem..
*ahem.. you don't need to put an exclamation point at your sentence.
There are two words with a "count" on it so I don't know which is which..
If you mean by the count in the "msgbox k.recordcount", then just try this code:
Code:
Public Sub Count()
Dim k as New ADODB.Recordset
Set k = New ADODB.Recordset
k.Open "SELECT COUNT(column_name) AS items FROM table_name", con1, 1, 2
msgbox rs!items
End Sub
This SQL statement displays the number of items in the table.
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Feb 9th, 2011, 05:06 AM
#7
Re: connect to database Module ...Problem
Change the name of the sub from Count to something else (perhaps CountTable3Records).
The error is implying that there is a hidden built-in routine/property/etc called Count, which is causing VB to get confused. Using a "proper" name avoids that kind of issue.
Note that there are a multitude of other smaller problems with your code, which can be corrected like this:
Code:
Public Sub CountTable3Records()
Dim con1 As adodb.Connection
Set con1 = New adodb.Connection
con1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=test"
con1.Open
Dim k As adodb.Recordset
Set k = New adodb.Recordset
k.Open "select Count(*) from table3", con1, adOpenForwardOnly, adLockReadOnly, adCmdText
MsgBox k.Fields(0).Value
k.close
Set k = Nothing
con1.Close
Set con1 = Nothing
End Sub
The first change is explained by the article Why shouldn't I use "Dim .. As New .."? from our Classic VB FAQs (in the FAQ forum)
The .CursorLocation should not be set to Client side unless needed (such as for a DataGrid), because it slows things down and causes various issues with locking etc.
The next change is to close things when you have finished with them, otherwise you waste memory etc and cause database problems such as locking/corruption/etc.
There is no need to copy all data from the table into memory just to get a record count, instead simply ask the database to give you the count.
Using the right parameters for recordset.Open improves speed and more. For more information, see the article What do the parameters of the recordset.Open method mean? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
 Originally Posted by louvelle
k.Open "SELECT COUNT(column_name) AS items FROM table_name", con1, 1, 2
Using "magic numbers" like that is a bad idea, because it makes the code much harder to read/write, and therefore much more likely to have bugs/errors.
Typing the full enum name (such as adOpenForwardOnly) isn't that hard, because you only need to type the first few letters and press Ctrl-Space.
-
Feb 9th, 2011, 05:24 AM
#8
Re: connect to database Module ...Problem
@Si
I see.. I usually use the full enum name a couple of years ago. But our instructor told us it's just the same as the full enum name if we use numbers.
Thanks for the info!
Manny Pacquiao once posted in his twitter:
It doesn't matter if the grammar is wrong, what matter is that you get the message
-
Feb 9th, 2011, 05:38 AM
#9
Re: connect to database Module ...Problem
It does have the same effect when it runs (assuming you use the right numbers!), but I've lost count of the amount of times I've seen threads where using numbers causes problems - usually because somebody copied code from somewhere else in their program, but the numbers weren't right for the new code.
-
Feb 9th, 2011, 09:41 AM
#10
New Member
Re: connect to database Module ...Problem
Thank Mr.si_the_geek , You are the man ...^^ and thank u Mis louvelle...
thank you really for your advice ,,, it work now
sorry ,, but what the best site that it give a sql command ?
and see the code below how i can make the single code count two table or more ??
i write this one but it give me an error for the second count ?
Code:
Dim con1 As adodb.Connection
Set con1 = New adodb.Connection
con1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Owner\My Documents\Database2.mdb;Persist Security Info=False"
con1.Open
Dim k As adodb.Recordset
Set k = New adodb.Recordset
k.Open "select Count(*) from table1", con1, adOpenForwardOnly, adLockReadOnly, adCmdText
MsgBox k.Fields(0).Value
k.Open "select Count(*) from table2", con1, adOpenForwardOnly, adLockReadOnly, adCmdText
MsgBox k.Fields(0).Value
k.Close
Set k = Nothing
con1.Close
Set con1 = Nothing
Last edited by hope flower; Feb 9th, 2011 at 09:48 AM.
-
Feb 9th, 2011, 10:38 AM
#11
Re: connect to database Module ...Problem
In future please don't just say "an error", tell us what the error is, and which line of code it occurred on. In this case my educated guess is probably right, but guessing takes extra time, and can be wrong.
You are getting an error because you aren't closing the recordset before you re-open it, eg:
Code:
k.Open "select Count(*) from table1", con1, adOpenForwardOnly, adLockReadOnly, adCmdText
MsgBox k.Fields(0).Value
k.Close
k.Open "select Count(*) from table2", con1, adOpenForwardOnly, adLockReadOnly, adCmdText
MsgBox k.Fields(0).Value
k.Close
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|