-
Jul 28th, 2014, 04:39 PM
#1
Thread Starter
Enjoy the moment
[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
-
Jul 28th, 2014, 05:15 PM
#2
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.
-
Jul 28th, 2014, 06:32 PM
#3
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.
-
Jul 28th, 2014, 07:13 PM
#4
Re: how i check if a customer name exists from another form
Originally Posted by DataMiser
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.
-
Jul 28th, 2014, 08:52 PM
#5
Re: how i check if a customer name exists from another form
Originally Posted by jmsrickland
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.
-
Jul 29th, 2014, 12:34 AM
#6
Thread Starter
Enjoy the moment
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
-
Jul 29th, 2014, 12:53 AM
#7
Re: how i check if a customer name exists from another form
Originally Posted by salsa31
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.
-
Jul 29th, 2014, 01:10 AM
#8
Thread Starter
Enjoy the moment
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.
-
Jul 29th, 2014, 01:43 PM
#9
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.
-
Jul 29th, 2014, 01:45 PM
#10
Thread Starter
Enjoy the moment
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|