Solved

For bhess1

Posted on 2000-03-24
9
185 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

821 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