Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What am I doing wrong here??

Status
Not open for further replies.

katiekat

Technical User
Jun 6, 2000
300
US
This is what I want to happen: I open a form. I enter customer information. I then push a button that opens up another form where I enter the address for that company. Now, if there is already an address, the form will open up to the correct reccord. If there is no address, the form will open up with the Key already filled in. Basically, I want it to create a reccord for me if there isn't one there already.

I already asked this question and I got this code in response:

Code:
Dim StrLinkCriteria As String   'Declare the variable StrLinkCriteria
    Dim stDocName As String         'Declare the variable stDocName
    stDocName = "Section B"         'Define stDocName
    
    'If there is already a record corresponding to the ApplicantID in Section B
    'Open the form with that record
    If DLookup("[ApplicantID]", "[Section B]", "[ApplicantID]=" & Forms![SECTION A]![ApplicantID]) Then
    'Define StrLinkCriteria, open the form in edit mode and requery the source for the form
        StrLinkCriteria = "[ApplicantID] = Forms![Section A]![ApplicantID]"
        DoCmd.OpenForm stDocName, , , StrLinkCriteria
        DoCmd.Requery
    'Otherwise open the form in add mode and set the OpenArgs value to the ApplicantID
    Else
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.OpenForm stDocName, , , , acAdd, , Me!ApplicantID

Now, this concept makes sense to me, but I can't get it to work right. I know there is something that I am missing, and I don't know what it is. Here is what I have, with all the stuff filled in.

Code:
Private Sub open_address_form_Click()
On Error GoTo Err_open_address_form_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "BusinessAddress"
    
    stLinkCriteria = "[Company ID]=" & "'" & Me![Company ID] & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    
    If DLookup("[Company ID]", "[businessaddress]", "[company id]=" & Forms![business informaion]![Company ID]) Then
    
    strlinkcriteria = "[company id] = forms![businessinformaion]![company id]"
    DoCmd.OpenForm BusinessAddress, , , strlinkcritera
    DoCmd.Requery
Else
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm BusinessAddress, , , , acFormAdd, , Me![Company ID]


Exit_open_address_form_Click:
    Exit Sub

Err_open_address_form_Click:
    MsgBox Err.Description
    Resume Exit_open_address_form_Click
    
End If
End Sub

I know this is really long and annoying, but I would really appreciate any help that I can get. I have been slaving over this for quite a while. Thank you thank you thank you!!
 
OK, I'll give it a shot. :)

I am assuming that this is the code for the button you push after you work on the company info and want to get to the address. I am wondering why you have the following lines of code:

stLinkCriteria = "[Company ID]=" & "'" & Me![Company ID] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria

BEFORE you test using the DLookup line???

It would seem to me, and it looks like that was the original suggestion that you got, that the first thing you need to do is to do the DLookup, and then based on that, use the OpenForm command in one of two ways.



Kathryn


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top