Find Method

  • Thread starter Thread starter diaa
  • Start date Start date
D

diaa

Guest
Hi All,

I need use find Method with string field but I can't. error when I start compilation.
but when I put a integer field work fine.

the code source is:

Function Compare(Code, Qte As Integer) As Boolean
(if Code string don't work)

Dim RS As DAO.Recordset
Dim DB As DAO.Database
Set DB = Application.CurrentDb
Set RS = DB.OpenRecordset("Produits", dbOpenDynaset)
Dim cuml As Integer
RS.FindFirst "Réfproduit=" & CStr(Code) (ERROR)
cuml = 0
Do While (RS.NoMatch <> True) And (Not RS.EOF)
cuml = cuml + RS.Fields("Quantité Acheté").Value
RS.FindNext "Réfproduit=" & CStr(Code)
Loop
If Qte < cuml Then
Compare = True
Else
Compare = False
End If
RS.Close
Set RS = Nothing
Set DB = Nothing
End Function

Thanks in advance
 
I'm very new at VBA, but had to work on something similar. Why not try instead?:

RS.FindFirst "[Réfproduit]=" & CStr(Code)

It worked for me.


HTH,

pour info quantité est déminin :D
 
There is first a syntax error - you're converting Code to a string, but you're not saying it's a string in the SQL. Meaning, you're not wrapping your string value in single quotes.

Also, Access may have trouble with foreign characters. It's still good to put your field names in brackets.

Code:
RS.FindFirst "[Réfproduit]='" & CStr(Code) & "'"
 
Last edited:
First, if you get an error and need help please state what the error is. This makes it easier to understand the problem that you are having.

You are changing the code to string so you need to treat it as text which means you have to use the correct delimters.

You have:
Code:
RS.FindFirst "Réfproduit=" & CStr(Code)

You need either:

Code:
RS.FindFirst "Réfproduit = '" & CStr(Code) & "'"

Code:
RS.FindFirst "Réfproduit= """ & CStr(Code) & """"

The second one is the better of the two although some do find getting the correct number of quotation marks a problem. The basic rule is that for every ' you would use you replace it with two " - thus ' becomes ""

Of course, the Réfproduit field has to be a Text field. If it is numeric then there's no need to conver it to a string and your line should read:

Code:
RS.FindFirst "Réfproduit = " & Code
 

Users who are viewing this thread

Back
Top Bottom