Link to home
Start Free TrialLog in
Avatar of sahi0002
sahi0002

asked on

Rectifying visual basic code

There is a field called "Contract ID"
Contract ID is a unique value to each record.
But for my new records that are entered Contract ID field is no more used instead a field  "FO No" is used.
Each Contract ID/FO No can have many vendors that is a one to many relationship.
For that a subform was created in the main form to view the vendors for each contract id/fo no.
Given below is the code I have for linking the subform and and main form with the
contract id/fo no.

The problem is that it only works when the contract id/fo no is a number like 12345
but when it is anything like abc123 then it shows me "Type Mismatch"
Both the fields in both my tables are in "Text" form.

I think it is something to do with:-
Dim lngID As String

Please help
Private Sub Command281_Click()
On Error GoTo Err_Command281_Click
 
   Dim strWhere As String
Dim lngID As String
 
If "" & Me.Contract_ID = "" Then
       strWhere = "[FO No]=" & Me.FO_No
       lngID = Me.FO_No
 
Else
      strWhere = "[Contract ID]=" & Me.Contract_ID
      lngID = Me.Contract_ID
 
End If
 
DoCmd.OpenForm "Job Request1", , , strWhere
If "" & lngID = Me.FO_No Then
 
Forms![Job Request1].[FO No].DefaultValue = lngID
Else
Forms![Job Request1].[Contract ID].DefaultValue = lngID
 
 
    End If
 
Exit_Command281_Click:
    Exit Sub
 
Err_Command281_Click:
    MsgBox Err.Description
    Resume Exit_Command281_Click
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tbsgadi
tbsgadi
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sahi0002
sahi0002

ASKER

Hi Gary,

I tried doing it but it still is the same.

My subform is basically in form of a button on the main form which opens. At some areas it opens up a box asking me to "Enter a parameter value" and at others it says:
Syntax error (missing operator) in query expression '[FO No]=abc12345'.
SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops - must have nodded off there!
Hi guys,

I tried it again (before i must have not copied the code correctly)
But its still not working perfectly.

I used:-
strWhere = "[FO No]='" & Me.FO_No & "'"

 strWhere = "[Contract ID]=" & Me.Contract_ID

Since the places where i have a contract id, it was giving me type mismatch error with
 strWhere = "[Contract ID]='" & Me.Contract_ID & "'"

Now for records where contract id is blank and there is FO No, it is opening up the subform (which previously wasn't and showing me error).
But the FO No value should appear on that column.
It is showing me:-
#Name?


I meant (before i must have not copied the code incorrectly)*
The FO No value is only appearing when the FO No is something like 12345 not with abc12345
what does If "" & lngID = Me.FO_No mean?
oh it should be If  lngID = Me.FO_No Then

I wanted that the whenever the contract is field is blank then it should look up the FO No field
also Forms![Job Request1].[FO No].DefaultValue = lngID and Forms![Job Request1].[Contract ID].DefaultValue = lngID
was used so that what ever value is there on the main form appears on the subform for either contract id or FO No depending if the contract is field is blank or not.
so that the user does not have to manually type in the value looking from the subform.
Hi guys,

I have added the sample database for you to see.

On the main form there is an are "Job Request" under which there is a button to open the subform.
If you see, if the field FO No is just a number without and alphabets, it is working fine but if there is a something like saifh23141 then it doesnt work.

Please solve my problem :(
PB-Listing-sample.zip
<<It is showing me:-#Name? >>
If you enter sa12243 into the actual Table then it works