Link to home
Start Free TrialLog in
Avatar of plimpias
plimpiasFlag for United States of America

asked on

Button to pick record..

I have a form that list company names, all the records have a button called view. The button opens the projects that are a part of the company.

Here is the code for the button

Sub ToggleLink_Click()
On Error GoTo ToggleLink_Click_Err

    If ChildFormIsOpen() Then
        CloseChildForm
    Else
        OpenChildForm
        FilterChildForm
    End If

ToggleLink_Click_Exit:
    Exit Sub

ToggleLink_Click_Err:
    MsgBox Error$
    Resume ToggleLink_Click_Exit

End Sub


This part works fine. It is filtering the projects that belong only to the company i pick
But i am trying to add a new button on this form that will bring up that project # in my project # form so i can edit it? how can i do this.

I was trying

Private Sub Command3_Click()
    Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "New_Project"
   
    stLinkCriteria = "[Project #]=" & "'" & Me![Project #] & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Form![New_Project]![Project #].Visible = True
   

Exit_Command25_Click:
    Exit Sub
   
Err_Command25_Click:
    MsgBox Err.Description
    Resume Exit_Command25_Click
   
End Sub

but that prompted my for a project # then errored out.
Avatar of will_scarlet7
will_scarlet7

Check out this thread:
    https://www.experts-exchange.com/questions/21111240/OPENING-FILTERED-FORM-FROM-A-UNION-QUERY.html

I think it is discussing exactly what you are trying to do.

God bless!

Sam
Try changing your syntax from this
    Form![New_Project]![Project #].Visible = True

to this
    Forms![New_Project]![Project #].Visible = True

It might be what is giving you the error...

Cheers
M
Avatar of plimpias

ASKER

Gives me the same thing says enter paremeter value, aks for project # then if i enter one in it give a run time error. You tried to lock table " while opening it, but the table cannot be locked becuase it is currently in use. Wait a moment, and then try the operation again.
What is the "Record Locks" property of your "New_Project" form set to?
Allrecords. Does it matter if the project # is in a subform within the New_Project form?
Just a guess from reading the error that you posted...
Change the Record Locks property for your form (New_Project), to either "No Locks" or "Edited Record" and see if that fixes it.

You might also need to change the Record Locks property on your form that is "filtering the projects that belong only to the company i pick"
I think it is that one of your other forms is using that same record, therefore it cannot open another form that has Allrecords locked while the first form is open.
OK i changed to no locks. the form where in button was had no locks on. Now im getting a error can't fine project # referred to in your expression? how can i point this expression to point to my subform and not my main form? the main form doesn't have the project # field in it, the subform does. also will this fix the peremeter value box it get?

Increasing this to 150...thanks
The proper syntax for pointing to a Subform is this

Forms!Mainform!Subform1.Form!Subform2.Form!ControlName
Sorry that's if you have two embedded subforms.  Here's the syntax for 1 subform
Forms!Mainform!Subform1.Form!ControlName
Something like this?

    stLinkCriteria = "Forms!Mainform!Subform1.[New_Project]![Project #]=" & "'" & Me![Project #] & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Forms!Mainform!Subform1.[New_Project]![Project #].Visible = True

Cheers
M
If this is the case:
> Does it matter if the project # is in a subform within the New_Project form?

You need something like this:
stLinkCriteria = "[Project #]=" & "'" & Me![Project #] & "'"
DoCmd.OpenForm stDocName
Forms(stDocName)!MySubformControl.Form.Filter=strLinkCriteria
Forms(stDocName)!MySubformControl.Form.FilterOn=True

I assume your main form is unbound, or the subform and main form aren't related on the Project # field. MySubformControl needs to be the name of the control that the subform is contained in, not the subform's name.
I get compile error:
Syntax error

Here is the code i am using  

Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "New_Poject"
   
    stLinkCriteria = "Forms!New_Poject!SF-New_Poject.[New_Poject]![Project #]=" & "'" & Me![Project #] & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    Forms!New_Poject!SF-New_Poject.[New_Poject]![Project #].Visible = True

I created my form with the wizard. It is 2 fields from two different tables. It is a linked type form.

Raisen the points to 250

Thanks for the posts
plimpias, have you tried the code I gave you?
No i didn't i'll try it right now. What do you mean by the name of the control that the subform is contained in?
Design the main form. Click on the subform and bring up the Properties window (View->Properties). Check that the title of the Properties window says "Subform/Subreport: <something>". Click in the Other page. The Name property is the name of the subform control. Basically, when a form is a subform, it sits inside a subform control, and that's what you need to refer to in this case.
I noticed that the name was different. But i changed it to SF-NewProject

the code now reads as

Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "New_Project"
   
    stLinkCriteria = "[Project #]=" & "'" & Me![Project #] & "'"
DoCmd.OpenForm stDocName
Forms(stDocName)!SF-NewProject.Form.Filter = strLinkCriteria
Forms(stDocName)!SF-NewProject.Form.FilterOn = True

Its giving a compile error (santax error) and highlights the row
Forms(stDocName)!SF-NewProject.Form.Filter = strLinkCriteria
ASKER CERTIFIED SOLUTION
Avatar of shanesuebsahakarn
shanesuebsahakarn
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
OK it works great but for some reason i have to hit the button twice in order to get the form to display the poject #. If i hit it once it opens the form and it is blank, then i hit it again and it opens the project #, after it does the first hit blank i can scroll through the rest of them fine.
When you say it's blank, do you mean it has no records in it, or it's completely blank (i.e. no controls or anything on it at all)?
no just no records in it...and when i hit the button again...the second time the record shows up correclty. I think the form has to be open for the code to work. Because when it is closed it doesn't work. After it is open it works good.
What's the recordsource of the main form, and how is it linked to the subform (i.e. what are the child and master fields)?
Ahh data entry was set to yes on the form. Damn that data entry!

Thanks for the help