Full Version: Button Question
UtterAccess Forums > Microsoft® Access > Access Forms
tonyba1976
Hi folks,
I have an Access "button" question for you.
I'm making a form with a button on it. Lets call it Form A. Form A uses
Table A. This button will cause a new type of form to appear that is
attached to a different table. Lets call this new form, Form B and this other table, Table B
respectively.
How do I make it so that when I press the Form A button, it takes the
value thats in a specific field in Form A and places it in a specific field in Form B. Also,
how do I make it so that when the button opens Form B, it causes a new record to be
created in Table B. In other words, how do I make it so that the Form A
button causes Form B to open up at the end of Table B?
I would appreciate any help that can be given.
Thanks
Tygler1
Welcome,
irst what I would do when Button A is pressed I would have it Hide form A(Not Close it). Then have it open form B it Add New record mode. Then you can pass any of the values from Form A to Form B by making the form B's Default Value = forms.formA.contol
Ocan help with the code and an example if need be...
quest4
Welcome to UA. Try something like like this in the OnClick event of the cmdButton:
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "FormB"
DoCmd.OpenForm stDocName, , , stLinkCriteria
DoCmd.Close ACForm, "FormA"
Then in FormB, in the form's OnOpen event try:
DoCmd.GoToRecord , , acNewRec
hth.
dannyseager
try

docmd.openform "formb"
docmd.gotorecord acDataForm ,"formb",acnewrec
forms![formb]![fieldB] = me.[fieldA]
tonyba1976
Thanks for the response Tygler1.
I'm new to the code writing part of Access. I would appreciate any examples or help with code that you can give.
To be more specific lets call the field in Form A that I want the button to get information from, Field A. Lets call the field in Form B that I want Button A to send information, from Field A, to Field B. Therefore I'm trying to get the Form A button, Button A, to take the information in Field A and place it into Field B of Form B. It should also start a new record in Table B on Form B.
How do I get Button A to hide Form A? How do I open Form B in Add New record mode? Do I use the button wizard?
Thanks again.
tonyba1976
Thanks for the warm welcome quest4.
How do I get to the OnClick event of the cmdButton?
tonyba1976
Thank you for replying danny.
Is this what I should type in the OnClick event of the cmdButton?
dannyseager
yes, if you type that in your onclick event for your button it will hopefully do what you need... I have commented to code below to help you understand it...
Opens the form called formb
docmd.openform "formb"
'goes to a new record on formb
docmd.gotorecord acDataForm ,"formb",acnewrec
'Makes the value of the field on formB = to the field on formA. Change the form and field names to suit.
forms![formb]![fieldB] = me.[fieldA]
after this you could then automatically close formA if you want to by using
docmd.close acdataform, "formA"
quest4
A simple thing I have been doing is put this in all of the forms OnOpen events:
DoCmd.Maximize
Simply go to the upper left hand corner of the form and right right mouse click the little square and select property. Then select the event tab and find the event you want to modify, click to right right of the txtbox and three dots should appear, ..., click on them and select code builder and then you can enter your code there. Then all of the forms open maximized, the formB covers FormA completely. Other than that, Danny's code will do everything you want in less code than mine. As you can see Danny is a pretty sharp guy. hth.
dannyseager
thanks guest4.. not that sharp.. just lazy... as little code as possible is best frown.gif
Dom DXecutioner
Welcome to utteraccess,

Although you have been given excellent suggestion by this fine members, below is another possible way to accomplish your task by using the form's OpenArgs property ...
This code would go on the OnClick event of a command button in the first form ...
compute.gif
CODE
Private Sub Command1_Click()

[color="green"]'// Dimension Variable(s)[/color]
Dim stDocName As String
Dim stOpenArgs As String

    [color="green"]'// assign name of form to open[/color]
    stDocName = "FormName"
    [color="green"]'// assign the value to be transfered[/color]
    stOpenArgs = "YourValue

    [color="green"]'// open form and pass arguments[/color]
    DoCmd.OpenForm stDocName, , , , , , stOpenArgs

End Sub

now on your second form you could have something like ...
compute.gif
CODE
Private Form_OnOpen(Cancel As Integer)

    [color="green"]'// check if any arguments have been passed[/color]
    If Not IsNull(Me.OpenArgs)

        [color="green"]'// assuming we have arguments,
        '// move to new record[/color]
        DoCmd.GoToRecord , , acNewRec
  
        [color="green"]'// apply your arguments[/color]
        Me.FieldName.Value = Me.OpenArgs

    End If

End Sub
quest4
Good suggestion, XyonX. I didn't even think of OpenArgs, but sense you mentioned it, it looks real good. Thanks, I will have to try and remember that.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.