Solved

For bhess1

Posted on 2000-03-24
9
186 Views
Last Modified: 2010-05-02
I'm using vb6 and an access dbase (97 format). Before I add a new record to the dbase I want to check for duplicates. The new record to add is located in a textbox. Before the text in the txtbox is added I want to check my table to see if the record already exists. I'm having trouble figuring out the sql query I would use. So far this is the sql query i'm using. but how do I test to see if the sqlstring found an existing record?

sqlstring = "Select RoomName from AssetRoom Where [RoomName] LIKE '*" & txtQuote & "*'"
de.conScheduler.Execute sqlstring

tnks.Meg.
0
Comment
Question by:Megan
  • 3
  • 3
  • 3
9 Comments
 

Author Comment

by:Megan
ID: 2655376
Adjusted points from 25 to 100
0
 

Author Comment

by:Megan
ID: 2655377
ahhh. now i understand the points system! sorry bhess1
I just went and answered a survey and so now i can/have increased the points for this question.   now i'm catching to how this works, heh heh..
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2656156
Welcome aboard Megan.. <smile>

When you do a point assignment.. DON'T add a question.. no one will answer but the person whose name appears!!.. LOL. If you want to do a question.. do a question.. because then everyone will reply to you. If you just want to reward points, in the text of the question just write a Thank You for answering the question.. or other mushy schtuff like that. Since I got you into this mess <sheesh> and it doesn't appear that bhess1 is around.. here's an answer for ya anyway.. <smile>.

But don't mix let it happen again, k?.. <lol> and a <wink>.

--------------------------------------
Below is an example of how you could do what you want. The syntax to use the Function would be..

strRoomName = "MyRoomName"
If xRoomNameExists (strRoomName, conScheduler) _
  Msgbox(strRoomName & " EXISTS")
Else
  Msgbox(strRoomName & " DOES NOT EXIST")
End If

<----- Code Begin ----->

Private Function xRoomNameExists _
(ByVal strRoomKey As String, ByRef cnScheduler As Adodb.Connection) _
As Boolean

  Static booInitialized As Boolean
  If Not booInitialized _
  Then
    booInitialized = True
    ' Define Recordset
    Static rsScheduler As New Adodb.Recordset
    With rsScheduler
      .ActiveConnection = cnScheduler
      .CursorLocation = adUseServer
      .CursorType = adOpenKeyset
      .LockType = adLockOptimistic
      .MaxRecords = 1
    End With
  End If
 
  With rsScheduler
    .MaxRecords = 1
    .Source = "Select RoomName From AssetRoom " _
      & "Where RoomName = '" _
      & strRoomKey _
      & "'"
    .Open , , , , adCmdText
    If .RecordCount > 0 _
    Then
      xRoomNameExists = True
    Else
      xRoomNameExists = False
    End If
    .Close
  End With

End Function

<----- Code End ----->
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 32

Accepted Solution

by:
bhess1 earned 100 total points
ID: 2661139
wsh2 has a good answer.  The key points:

If you need to find identical matches, don't use LIKE.  If you can't have anything similar, then LIKE is correct, but you need to put other limits on it (e.g. RoomName LIKE '*a*' would find all names with an a in them)

You either need to return a recordset (assign the output of the query to a recordset), or use a stored procedure that returns a value (like a recordcount, or a COUNT() of the names that match).  The Recordset is easier - you would modify your query to be:

sqlstring = "Select RoomName from AssetRoom Where [RoomName] LIKE '*" & txtQuote & "*'"

Dim tRS as ADODB.RecordSet

Set tRS = de.conScheduler.Execute sqlstring

If tRS.RecordCount <> 0 Then ' has matching records
....

wsh2 used a .MaxRecords on his query.  Personally, I like using a TOP N query, e.g.

Select TOP 1 Roomname FROM AssetRoom WHERE RoomName = '" & txtQuote & "'"

Either one limits the number of records returned, saving you resources on your query processing and data transfer.  In my example above (LIKE '*a*'), you could end up transferring hundreds of records, but with TOP 1 or .Maxrecords = 1, you will only send one record.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2661511
Bhess1:
Hmmmmm.. You now have me curious.. I wonder which is more efficient.. .Maxrecords or TOP? Oh nevermind.. its a rhetorical question.. besides Who knows or actually cares?... <LOL>.

But I do have a question bhess1. Originally I wanted to use the COUNT() keyword, but then suddenly realized I don't know how to use it in VB !!! <blush> <blush>.. As it returns nor changes any records.. how can VB employ its use?
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2661702
"Select COUNT(MyField) as RecCt From MyData"

Don't you feel silly now?
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2661911
Bhess1:
DUH !!! The question is, how do you address the resulting count field in VB?.. Oh Geez.. I just saw it.. recordset!count, huh?.. Ok.. silly.. works for me.. LOL and a <blush>.. Thank YOU.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2662149
You got it!  rs!RecCt  Have fun
0
 

Author Comment

by:Megan
ID: 2662253
Thanks Guys, once again! :)

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 73
How to debug this code 7 64
Recommendation vb6 to vb.net or others 14 178
Problem to line 23 55
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question