Put stable Value in a form from another form

Hamatto

Member
Local time
Today, 11:16
Joined
Jun 17, 2024
Messages
40
Hello Friends,
Please I want your help in the attachement file
I have Form1 has two buttons A and B. I want to open Form 2 by two ways
1- by click on A the Form 2 open with:
- Title label(Grade One) and the comboBox show(One)(Note: I will make This combobox invisible) and then the textbox( Beside Class) will be 1
- I want That be Stable whenever I enter data in this Form2( Because when I add new Student that changed to null)
I mean the class still 1/.... when I add any Student by using this form
and if there are already Students Then Students who are in Grade one only shown when navigating in this form

2- the same of 1- but by click on B the Form 2 open with: Grade Two and 2/ not change when I add new Students

Any help will be really appreciated
Thank a lot for you all
 

Attachments

Just make that the default value for that control?
 
Just make that the default value for that control?
Thanks a lot for reply I tried that but I want this default value changed by Vba Code. So, when click button A Form2 opened with defult value 1 and when click button B Form2 opened with defult value 2
 
try something along the lines of:

Code:
DoCmd.OpenForm "Form2", , , , acFormAdd
Forms("Form2").SomeText.DefaultValue = "1234"
 
try something along the lines of:

Code:
DoCmd.OpenForm "Form2", , , , acFormAdd
Forms("Form2").SomeText.DefaultValue = "1234"
Thanks for reply
I am sorry that's not worked with me
 
Thanks for reply
I am sorry that's not worked with me
I'll assume you changed my sample code above with your control names and default values. "Not Worked" doesn't tell us much.

I looked at your sample and this works. Make sure you comment out the load event code in your form2.

Code:
Private Sub cmd_g1_Click()

    Dim strFrmName As String
    strFrmName = "Form2"

    DoCmd.OpenForm strFrmName

    With Forms(strFrmName)
        .lbl_Grade.Caption = "Grade One"
        .cmb_Grade.DefaultValue = 1
    End With

End Sub

Private Sub cmd_g2_Click()

    Dim strFrmName As String
    strFrmName = "Form2"

    DoCmd.OpenForm strFrmName

    With Forms(strFrmName)
        .lbl_Grade.Caption = "Grade Two"
        .cmb_Grade.DefaultValue = 2
    End With

End Sub
 
Thanks for reply
I am sorry that's not worked with me
When you say 'Not worked' show us what you used to get that result.
As mentioned 'Not worked' is as much use as a chocolate fireguard. :)
 
Hello friends,
I tried many things, But I get the following :
When I open form1 and click Button A then Form2 opened with Title (Grade One) and get One (in cmb_grade) and 1 (in txt_Grade) which is very good.But the problem is when I open form1 and click Button B and get Form2 opened with Title (Grade Two) Then I did't get Two (in cmb_grade) and 2 (in txt_Grade) Which I want too.
Please,I put the file in attachement to any help
Any Help will be really appreciated
Thanks a lot


When you say 'Not worked' show us what you used to get that result.
As mentioned 'Not worked' is as much use as a chocolate fireguard. :)
Thanks a lot for reply
Excuse me, I attachement the file to show what I do and what happen
 

Attachments

I do not want to have to keep downloading files just to see a snippet of code. :(

Please post the code within code tags (the </> icon)
 
Walk your code with F8 and breakpoints.
You are putting the cart before the horse. :(

You are testing something, then setting it later, which is too late.
Google OpenArgs which would be one method, or set the value from Form1 as yoiu do the caption?
However as you have no parent for 2 you have get your data correct.
It is showing 1 all the time as that is what is in the first record of that Form2.

If you create a new record then that DefaultValue will then take effect.

Also please so not crosspost without advising each site that you have done so, else you will likely lose assistance from people like myself. :(
 
Well of course it does not work for b since you have this code in form2. What is the purpose? You cannot have both codes.
Code:
Private Sub Form_Load()
  Dim x As Integer
  x = cmb_Grade.ListCount - 1
  Select Case Me.lbl_Grade.Caption
  Case "Grade One"
  Me.cmb_Grade = Me.cmb_Grade.ItemData(0)
  Case "Grade Two"
  Me.cmb_Grade = Me.cmb_Grade.ItemData(x)
End Select

Also I assume you want this form to open in Add new. If not you simply overwrite the first record each time.

Here is the working Solution. There are many problems with Form 2 and additional code. The code references controls and fields that do not exist on these forms.

Code:
Private Sub cmd_g1_Click()
 Dim strFrmName As String
 strFrmName = "Form2"
 DoCmd.OpenForm strFrmName, , , , acFormAdd
 With Forms(strFrmName)
      .lbl_Grade.Caption = "Grade One"
      .cmb_Grade = 1
      .cmb_Grade.DefaultValue = 1

End With
End Sub

Private Sub cmd_g2_Click()
Dim strFrmName As String
strFrmName = "Form2"
DoCmd.OpenForm strFrmName, , , , acFormAdd
With Forms(strFrmName)
      .lbl_Grade.Caption = "Grade Two"
      .cmb_Grade = 2
      .cmb_Grade.DefaultValue = 2
End With
End Sub

In Form 2
Code:
Private Sub cmb_Grade_AfterUpdate()
  Select Case Me.cmb_Grade
  Case 1
    Me.lbl_Grade.Caption = "Grade One"
    Me.cmb_Grade.DefaultValue = 1
  Case 2
    Me.lbl_Grade.Caption = "Grade Two"
    Me.cmb_Grade.DefaultValue = 2
  End Select
End Sub
 

Attachments

Thanks a lot MajP That's very good. Is that suitable with add and after that when navigate what I added before?

Thanks a lot Gasman for your advice and Sorry for Crosspost(I really didn't know that is wrong)
 
It is not wrong per se, but considered good manners to advise each site, so help is not duplicated and people waste their time.
 

Users who are viewing this thread

Back
Top Bottom