Solved

For bhess1

Posted on 2000-03-24
9
181 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now