Solved

For bhess1

Posted on 2000-03-24
9
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

691 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