Results 1 to 10 of 10

Thread: [RESOLVED] how i check if a customer name exists from another form

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how i check if a customer name exists from another form

    i am trying to check if a customer name exists but something missing in my code
    it doesnt work properly
    Code:
        Dim RsC As New ADODB.Recordset
        Set RsC = New ADODB.Recordset
            
            Dim strName As String
            strName = FrmNotPaid.LsVw.SelectedItem.SubItems(2)
            StrSql = "select FullName from Customers where FullName ='" & RplS(strName) & "'"
            RsC.Open StrSql, CN, adOpenStatic, adLockOptimistic
        If rs.RecordCount < 0 Then
        MsgBox "Customer Dosnt Exists in the DataBase"
    Else
    
    FrmCustPaySlip.Show 1
    
        End If
    tnx for the help

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: how i check if a customer name exists from another form

    Make strName Public in a .Bas module


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how i check if a customer name exists from another form

    There is no need to make it public. It is both defined and assigned in the code shown. Making it a module level public var would just be a waste of resources and would not help the problem.

    on the other hand

    Code:
    If rs.RecordCount < 0 Then
    Do you really think that there can be less than 0 records in a recordset?

    Of course there is no way to know what you mean by "doesn't work properly"
    You really need to be more descriptive.

    Also note that if you expect that to be found more often than not then you should have the reverse logic in your if statement.

    Code:
    If MostLikelyCondition Then
         'code that will execute most of the time
    Else
         'Code that executes when the condition is not what it will be most of the time
    End If
    You always want the code that is more likely to be executed as the first part of an If statement and not the Else portion. While it is not a huge deal and either will work it is better to develop the habit of doing it the more efficient way.
    In other words using your existing code it should be modified as
    Code:
        If rs.RecordCount > 0 Then
            FrmCustPaySlip.Show 1
        Else
            MsgBox "Customer Dosnt Exists in the DataBase"
        End If
    That said recordcount is not always available, depends on the cursor type used
    Would be better to check the .EOF instead
    Code:
        If Not rs.EOF Then
            FrmCustPaySlip.Show 1
        Else
            MsgBox "Customer Dosnt Exists in the DataBase"
        End If
    It would also be a good idea to correct the spelling and grammar mistakes

    MsgBox "Customer Dosnt Exists in the DataBase"
    should be
    MsgBox "Customer doesn't exist in the database"
    Last edited by DataMiser; Jul 28th, 2014 at 06:35 PM.

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: how i check if a customer name exists from another form

    Quote Originally Posted by DataMiser View Post
    There is no need to make it public. It is both defined and assigned in the code shown.
    I don't understand your statement

    If it is defined in the code with Dim then how can it be looked at in another Form?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how i check if a customer name exists from another form

    Quote Originally Posted by jmsrickland View Post
    I don't understand your statement

    If it is defined in the code with Dim then how can it be looked at in another Form?
    It is not being looked at in another form. It is set to the value of something from another form and then used in a query.
    The variable is not even needed but it is being used and used properly.
    Perhaps you should look at the code posted again.

    Of course it is possible that something needs to be passed to the other form that is called when the query returns a match but given the lack of info in the OP can't say.
    Last edited by DataMiser; Jul 28th, 2014 at 08:55 PM.

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how i check if a customer name exists from another form

    Of course there is no way to know what you mean by "doesn't work properly"
    i mean that it dosnt check to see if the name exists in the customer table and still let me modify what i should not modify

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: how i check if a customer name exists from another form

    Quote Originally Posted by salsa31 View Post
    i mean that it dosnt check to see if the name exists in the customer table and still let me modify what i should not modify
    Which is the first thing I pointed out. .. well after I mentioned that the public var would not solve anything.

    There will never be < 0 records returned so the Else statement will always execute no matter if it finds no records or 100 records.

    To put it another way....

    This code
    Code:
        
        If rs.RecordCount < 0 Then
        MsgBox "Customer Dosnt Exists in the DataBase"
    Else
    
    FrmCustPaySlip.Show 1
    
        End If
    Is functionally the same as
    Code:
    FrmCustPaySlip.Show 1
    See my first post for the correct way to do it
    Last edited by DataMiser; Jul 29th, 2014 at 12:56 AM.

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how i check if a customer name exists from another form

    well i tried this sir and it seems to work
    Code:
     If (RsC.EOF And RsC.BOF) Then
    tnk you for the help
    tnx james
    Last edited by salsa31; Jul 29th, 2014 at 05:09 AM.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] how i check if a customer name exists from another form

    I showed you two other ways to do it. There is no need to check both the EOF and BOF if EOF is true when you open a recordset then there are no records in it.

    Did you at least understand what I meant by properly structuring an If statement so that the true part is what will run most of the time and Else part will be the exception?

    Since you did not bother to tell us whether you expect to find the customer there most of the time or not then I can't say if you are doing it the best way or not.

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [RESOLVED] how i check if a customer name exists from another form

    Did you at least understand what I meant by properly structuring an If statement so that the true part is what will run most of the time and Else part will be the exception?
    yes sir i did
    tnk you for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width