Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / March 2008



Tip: Looking for answers? Try searching our database.

VB help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick - 22 Mar 2008 14:00 GMT
Hi im currently having a problem with taking data from a field in a database
and then using it within the form, when supplied with a telephone number by
inputting in
to a text box, the number needs to be checked against the database and then
if a record matches the telephone number the Name in the record shoudl be
inserted into another text box.

I am using visual basic 6 and microsoft access 2000

At the moment i am getting an error of "the connection cannot be used to
perform this operation"

Current Code

Dim CON As Connection
Dim rs As New Recordset
Dim SQLname As String

Private Sub cmd_details_Click()

rs.Open "Select * from customer, CON, adOpenKeyset, adLockOptimistic,"

SQLname = "Select name from Customer where telephone='" & (txtphone.Text)
txtname.Text = SQLname

End Sub

Private Sub form_initialize()

Set CON = New ADODB.Connection      'set the database connection
CON.Open "Provider=Microsoft.jet.oledb.4.0; Data Source=" & App.Path &
"\test.mdb"  'connection string to the database

End Sub

thanks in advance
Ralph - 22 Mar 2008 18:22 GMT
> Hi im currently having a problem with taking data from a field in a database
> and then using it within the form, when supplied with a telephone number by
[quoted text clipped - 30 lines]
>
> End Sub

1) Is this the correct code or something you typed from your code? Always
use Copy 'n Paste. That way we can tell typos from actual errors.

2) This line shows the entire argument as a quoted string which is wrong.
> rs.Open "Select * from customer, CON, adOpenKeyset, adLockOptimistic,"
It should be ...
rs.Open "Select * from customer", CON, adOpenKeyset, adLockOptimistic

3) This line makes no sense, ie it is merely displaying the entire SQL
string in the textbox.
> SQLname = "Select name from Customer where telephone='" & (txtphone.Text)
> txtname.Text = SQLname
Perhaps what you really want to do is make a new query against the database?
Also the enclosing tick (single-quote character) is missing from the end of
the SQL statement.
It should be ...
SQLname = ""Select name from Customer where telephone='" & _
                       txtPhone.Text & "'"    ' include closing tick

4) Use specific Error Handling. ADO often returns additional errors in its
Error Collection that can better help you understand what is going on.
http://support.microsoft.com/kb/167957
http://support.microsoft.com/kb/168336
Download this free tool to make it easier to instrument error handling in VB
http://www.mztools.com/v3/download.aspx

5) While not a problem in this case, and often used in *sample* code, you
should not use the "As New" construct to Dimension Object References in your
production code. It tends to obscure errors.
Also you should always fully qualify all objects.
And never use parentheses in VB as a unary operator unless you need to, and
you know why you need to. It often doesn't do what you think it does. <g>

Your code should look like this ...

Dim rs As ADODB.Recordset    ' not newed, fully qualified
Private Sub cmd_details_Click()
On Error Goto DetailClick_Err
    Set rs = New ADODB.Recordset
    Dim SQLname As String   ' building the sql statement outside the call
                                             ' makes it easier to view and
validate
   SQLname = ""Select name from Customer where telephone='" & _
                       txtPhone.Text & "'"    ' include closing tick
   ' Debug.Print SQLname       ' review
    rs.Open SQLname, CON, adOpenKeyset, adLockOptimistic
       ' check to make sure we found something
       ' in this case grab the first one that shows up
       ' your code may be different
    While Not rs.EOF Or Not rs.BOF
          txtname.Text = rs.Fields("telephone").Value
          ' or use ...
          txtname.Text = rs!telephone
     Wend
     Exit Sub
DetailClick_Err:
      ' handle error here
      ' see above links for more robust error reporting
End Sub

Hope this helps

-ralph
Rick - 23 Mar 2008 00:56 GMT
>> Hi im currently having a problem with taking data from a field in a
> database
[quoted text clipped - 102 lines]
>
> -ralph

1) Yes this is the exact code i am currently using. Copied and pasted
directly
  from the form.

2) Thanks, i had missed that when i was looking over the code.

3) When i use ""select at the start VB doesnt like the syntax of two " in a
row
  is this correct or should it only be one ?

4) Now i have corrected all my code to what you have recomended. When a
number
  which is not stored in the database is eneterd nothing happens which is
good
  for now. When a number that is correct is enetered, the whole of visual
basic
  locks up and has to close down. This is however some form of progress.

Thanks for the help so far i really appreciate it.
Ralph - 23 Mar 2008 01:37 GMT
> >> Hi im currently having a problem with taking data from a field in a
> > database
[quoted text clipped - 112 lines]
> row
>    is this correct or should it only be one ?

Yes. Creating a query string doesn't absolutely nothing. It is only when the
query is used with a command or open to do something or return a recordset
is the string used.

> 4) Now i have corrected all my code to what you have recomended. When a
> number
[quoted text clipped - 3 lines]
> basic
>    locks up and has to close down. This is however some form of progress.

That's because I'm STUPID! <G>

You are caught in an endless loop because I forgot a very, very important
line...
    While Not rs.EOF Or Not rs.BOF
         txtname.Text = rs.Fields("telephone").Value
          ' or use ...
         txtname.Text = rs!telephone
         ' insert the following line
         rs.MoveNext     ' otherwise EOF or BOF will NEVER be true Duh!
    Wend

Please accept my apologizies.

-ralph
Rick - 23 Mar 2008 11:42 GMT
>> >> Hi im currently having a problem with taking data from a field in a
>> > database
[quoted text clipped - 158 lines]
>
> -ralph

Dont worry about it, the help you have given me so far has been excellent.

When I insert this field the correct data is displayed in the correct text
box, but then i get "run time error '3021': Either BOF or EOF is True, or
the current record has been deleted. Requested operation requires a current
record"
Ralph - 23 Mar 2008 12:15 GMT
> Dont worry about it, the help you have given me so far has been excellent.
>
> When I insert this field the correct data is displayed in the correct text
> box, but then i get "run time error '3021': Either BOF or EOF is True, or
> the current record has been deleted. Requested operation requires a current
> record"

Are you using the conditional loop I showed you?
Use "And" not "Or".

    While Not rs.EOF And Not rs.BOF
         txtname.Text = rs.Fields("telephone").Value
         rs.MoveNext
    Wend

Note: Using a loop like this means that if there are more than one matching
record you will end up with the last one. If you want to grab the first
record returned and then do something like this ...

  If Not rs.EOF And Not rs.BOF Then
       txtname.Text = rs.Fields("telephone").Value
  End If

I don't know what's wrong with me lately.

-ralph
Rick - 23 Mar 2008 12:09 GMT
>> >> Hi im currently having a problem with taking data from a field in a
>> > database
[quoted text clipped - 158 lines]
>
> -ralph

Using

Do Until rs.EOF
           If rs.EOF = True Then
                   rs.MoveLast
            End If
   txtname.Text = rs!Name
     rs.MoveNext
Loop

The code works correctly, with no errors, thanks for the help Ralph, its
been invaluable.

Thanks.
Ralph - 23 Mar 2008 12:29 GMT
> Using
>
[quoted text clipped - 10 lines]
>
> Thanks.

Don't thank me. I typed lousy code twice.

I should have slapped the "Air Code" label on it, since I was shooting from
the hip.

Only problem with the above loop, is that if the recordset is empty - it may
error in some situations - since the recordset could be BOF and not EOF.
This usually happens if you have been piddling a bit previously, or a 3am in
the morning. But best to never take it for granted, unlike my typing. <g>

-ralph
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.