Button Click Event to choose between forms to open based on TextBox input (1 Viewer)

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
HI,

Making a small database, Got stuck Help Needed.

Got 1 Table.

1. ContactDetailTable

Got 3 forms.
1. ContactIDForm
2. ContactInfoForm
3. NewContactFrom

In ContactIDForm it contain 1 textbox name 'TextBox' with Button Name 'Btn'

In ContactIDForm there is only 1 Text Box ContactIDTextBox and 1 Button. User Enters ID in TextBox and On Button Click Event it should check data from TextBox in Table name (ContactDetailTable) in field ContactID and if there is record matching, ContactInfoForm should Open else NewContactForm should open with ContactIDTextBox value in it.

Does it makes sense?
 

rzw0wr

I will always be a newbie
Local time
Yesterday, 23:52
Joined
Apr 1, 2012
Messages
489
This might work a little better with a combo box.

Dale
 

bob fitz

AWF VIP
Local time
Today, 04:52
Joined
May 23, 2011
Messages
4,718
What need would not be fulfilled by using a combo box.
 

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
What need would not be fulfilled by using a combo box.

It looks... Dropdown list... Dropdown button. It will have a single entry one time and new every time. Can we use it as a textbox?
 

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
It looks... Dropdown list... Dropdown button. It will have a single entry one time and new every time. Can we use it as a textbox?

Please understand as I only need a to get few numbers from that Text Box to check with tables etc.... do not need any list of some thing... thank you.
 

JHB

Have been here a while
Local time
Today, 05:52
Joined
Jun 17, 2012
Messages
7,732
Adding the following code to your button's click event.
If ContactID is a number field use this:
Code:
  Dim dbs As Database, rst As Recordset
  
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("SELECT ContactID FROM ContactDetailTable " _
  & "WHERE ContactID=" & Me.ContactIDTextBox)
  If Not rst.EOF Then
    DoCmd.OpenForm "ContactInfoForm"
  Else
    DoCmd.OpenForm "NewContactForm"
  End If
If ContactID is a text field use this:
Code:
  Dim dbs As Database, rst As Recordset
  
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("SELECT ContactID FROM ContactDetailTable " _
  & "WHERE ContactID='" & Me.ContactIDTextBox & "'")
  If Not rst.EOF Then
    DoCmd.OpenForm "ContactInfoForm"
  Else
    DoCmd.OpenForm "NewContactForm"
  End If
 

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
Adding the following code to your button's click event.
If ContactID is a number field use this:
Code:
  Dim dbs As Database, rst As Recordset
 
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("SELECT ContactID FROM ContactDetailTable " _
  & "WHERE ContactID=" & Me.ContactIDTextBox)
  If Not rst.EOF Then
    DoCmd.OpenForm "ContactInfoForm"
  Else
    DoCmd.OpenForm "NewContactForm"
  End If
If ContactID is a text field use this:
Code:
  Dim dbs As Database, rst As Recordset
 
  Set dbs = CurrentDb
  Set rst = dbs.OpenRecordset("SELECT ContactID FROM ContactDetailTable " _
  & "WHERE ContactID='" & Me.ContactIDTextBox & "'")
  If Not rst.EOF Then
    DoCmd.OpenForm "ContactInfoForm"
  Else
    DoCmd.OpenForm "NewContactForm"
  End If

Brilliant Thank you. But we still missing a small point. when a form loads it should show the ContactID entry in its ContactIDTextBox so we could know what user entered in first place.

Also can you explain me step by step how your code works? So I could learn and correct my self where I was mistaking.
 

JHB

Have been here a while
Local time
Today, 05:52
Joined
Jun 17, 2012
Messages
7,732
Then use open arguments:
Insert the code in the form NewContactForm open event:
Code:
Private Sub Form_Open(Cancel As Integer)
  Me.ContactIDTextBox = Me.OpenArgs
End Sub
And
Code:
   DoCmd.OpenForm "NewContactForm", , , , , , Me.ContactIDTextBox
in the code I gave you before.

Don't forget my signature. :D
 

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
Then use open arguments:
Insert the code in the form NewContactForm open event:
Code:
Private Sub Form_Open(Cancel As Integer)
  Me.ContactIDTextBox = Me.OpenArgs
End Sub
And
Code:
   DoCmd.OpenForm "NewContactForm", , , , , , Me.ContactIDTextBox
in the code I gave you before.

Don't forget my signature. :D


Thanks.. give me your email address... please. we can chat for future if you want......
 

u4jaanus

New member
Local time
Yesterday, 20:52
Joined
Jun 7, 2013
Messages
7
Thanks.. give me your email address... please. we can chat for future if you want......

Hay.. I am thinking to make a Form which main categories bottons on it, suppose for a student database.

I have main form name Category, with 1 Button named Students.
which triggers another form name Students with buttons Register new Student, Register Old Student. I am thinking if we could get some thing like... On click on Student Button on Category form... 2 more buttons appears in the same form so could use them to register new and Old....

Was thinking to put Subform...but not clear if can use it for all categories.
 

Users who are viewing this thread

Top Bottom