473,803 Members | 4,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enable subform based on combo box choice

AccessIdiot
493 Contributor
I have been looking through posts on events happening based on a combo box choice but none really get at the heart of what I am trying to do.

I think this is simple but I'm a newbie so any hand holding and non-startling movements will help here. :-)

I have a form Specimen with a dropdown that displays Species by Species_Code. In the Species table I have a Yes/No field for whether or not the Species is a Fish (Yes = fish). If the user chooses a Species_Code that is a Fish I want the subform to become enabled.

Seems like this is pretty simple but I don't know VB (though I can kind of follow it when I see it written) and am not sure on which event to put this on or how to tie a field from the Species table that isn't being displayed in the dropdown to the subform. Is that confusing?

Basically --> If user chooses a Species_Code that has Fish = yes for that record then subform.visible = true.

Help!
Melissa
Feb 27 '07 #1
26 5693
ADezii
8,834 Recognized Expert Expert
I have been looking through posts on events happening based on a combo box choice but none really get at the heart of what I am trying to do.

I think this is simple but I'm a newbie so any hand holding and non-startling movements will help here. :-)

I have a form Specimen with a dropdown that displays Species by Species_Code. In the Species table I have a Yes/No field for whether or not the Species is a Fish (Yes = fish). If the user chooses a Species_Code that is a Fish I want the subform to become enabled.

Seems like this is pretty simple but I don't know VB (though I can kind of follow it when I see it written) and am not sure on which event to put this on or how to tie a field from the Species table that isn't being displayed in the dropdown to the subform. Is that confusing?

Basically --> If user chooses a Species_Code that has Fish = yes for that record then subform.visible = true.

Help!
Melissa
I would have liked a little more information but I'll make some basic assumptions such as:
__1 Your Combo Box is named cboSpeciesCode.
__2 Your Species Table contains the Fields [Fish] (Yes/No), [Species] (Text), and [Species_Code] (Long).
__3 Your Sub-Form name is subfSpecies.
__4 Make any other adjustments as needed.
__5 In the AfterUpdate() Event of cboSpeciesCode:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSpeciesCode_AfterUpdate()
  2. Dim intSpeciesCode As Integer, IsItAFish As Integer
  3.  
  4. intSpeciesCode = Me![cboSpeciesCode]
  5. IsItAFish = DLookup("[Fish]", "Species", "[Species_Code]=" & intSpeciesCode)
  6.  
  7. If IsItAFish = -1 Then  'True - it is a fish
  8.   Me![subfSpecies].Visible = False
  9. Else
  10. End If
  11. End Sub
Feb 28 '07 #2
AccessIdiot
493 Contributor
Sorry! I should have specified these things. I made appropriate changes but Species_Code is text, not a long integer. So it seems to be getting hung up on this line:
Expand|Select|Wrap|Line Numbers
  1. IsItAFish = DLookup("[Fish]", "Species", "[Species_Code]=" & intSpeciesCode)
  2.  
What do I have to change? Also, could you explain what the line does so I know how to apply it next time?

Here is what I changed it to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_AfterUpdate()
  2. Dim intSpeciesCode As Integer, IsItAFish As Integer
  3.  
  4. intSpeciesCode = Me![Species]
  5. IsItAFish = DLookup("[Fish]", "Species", "[Species_Code]=" & intSpeciesCode)
  6.  
  7. If IsItAFish = -1 Then  'True - it is a fish
  8.   Me![subfrm_FishSpecimen].Visible = False
  9. Else
  10. End If
  11. End Sub
Thank you!
Feb 28 '07 #3
AccessIdiot
493 Contributor
More information:

Combo box is called "Species", Species Table has fields [Species_ID] (autonumber, PK), [Species_Code] (text), [Common_Name] (text), [Family] (text), [Genus_ID] (autonumber, FK), [Scientific_Name] (text), [Alternate_Name] (text), [Fish] (Yes/No).

Do I need to include all these in the code?

Subform name is sbfrm_FishSpeci men

Here is what I changed your suggested code to:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_AfterUpdate()
  2. Dim intSpeciesCode As Integer, IsItAFish As Integer
  3.  
  4. intSpeciesCode = Me![Species]
  5. IsItAFish = DLookup("[Fish]", "Species", "[Species_Code]=" & intSpeciesCode)
  6.  
  7. If IsItAFish = -1 Then  'True - it is a fish
  8.   Me![subfrm_FishSpecimen].Visible = False
  9. Else
  10. End If
  11. End Sub
Thank you!
Feb 28 '07 #4
AccessIdiot
493 Contributor
Anyone know how to change this if SpeciesCode is a string?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Species_AfterUpdate()
  2. Dim intSpeciesCode As Integer, IsItAFish As Integer
  3.  
  4. intSpeciesCode = Me![Species]
  5. IsItAFish = DLookup("[Fish]", "Species", "[Species_Code]=" & intSpeciesCode)
  6.  
  7. If IsItAFish = -1 Then  'True - it is a fish
  8.   Me![subfrm_FishSpecimen].Visible = False
  9. Else
  10. End If
  11. End Sub
  12.  
Obviously Dim SpeciesCode As String but how do I change the DLookup line?

Also, why is -1 a true value? Shouldn't it be just 1?

thanks for any help!
Feb 28 '07 #5
AccessIdiot
493 Contributor
Wow, how quickly this got buried!

Anyway, I am getting this error:

Run-time error '94':
Invalid use of Null

Here is my code now (I changed some table and form names)
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSpecies_AfterUpdate()
  2.     Dim SpeciesCode As String, IsItAFish As Integer
  3.  
  4.     SpeciesCode = Me![cboSpecies]
  5.     If IsNull(Me.cboSpecies) = False Then
  6.         IsItAFish = DLookup("[Fish]", "[tbl_Species]", "[Species_Code]= '" & Me.cboSpecies & "'")
  7.     End If
  8.  
  9.     If IsItAFish = -1 Then  'True - it is a fish
  10.         Me![subfrm_FishSpecimen].Visible = False
  11.     Else
  12.     End If
  13. End Sub
I have tried all sorts of things with the DLookup line and I get either the Inavlid use of Null error or a data mismatch error, no matter what I put in the criteria part of the function.

Any ideas?

thanks,
melissa
Feb 28 '07 #6
AccessIdiot
493 Contributor
Okay I got it! Finally! I was confused on the combo box and was using the value shown (text) vs the bound source (integer).

Anyway, it works beautifully.

HOWEVER, if I change the value in the dropdown the function doesn't refire. Sort of.

So if I choose a non-fish in the dropdown the subform doesn't show. If I change the dropdown to be a fish the subform does show (hurrah!). If I change the dropdown to a non fish again the subform is still there. How do I change that?

Basically, I want the subform to show only when the user chooses a fish. If they make a mistake and go back and change it to non-fish the subform needs to disappear.

Thanks!
Feb 28 '07 #7
NeoPa
32,579 Recognized Expert Moderator MVP
Melissa,
Give me some time (maybe over the weekend) and I'll come back to you on this one. I'm just leaving work atm.
There are a number of questions in your posts and I'd like to cover them as they all seem to point to your better understanding the subject, which is a bonus for all of us here :)
Mar 2 '07 #8
NeoPa
32,579 Recognized Expert Moderator MVP
Let's try this one at a time shall we.
I'll respond to some of your points then we'll see if we can take it from there.
True.
True is held as -1 because boolean fields are stored as numbers in a binary field. In a binary field, when all bits are set to one, the equivalent numeric value is -1. In an associated point, it is never a good idea to compare boolean fields.
IE. Never say If MyBoolean = True (or -1 even) Then... Always say If MyBoolean Then... instead.
Lack of Response.
This can happen sometimes and is usually the result of some confusion or some expert being away for a short while. You know from personal experience that it is unusual. However, if for any reason an expert is working on your thread and his notifications become reset (there is a bug in the site currently which is being looked at) then he will not be aware of your responses I'm afraid.
Knowing ADezii as I do, I'm sure he wouldn't leave you hanging like that deliberately. The Access forum is currently so busy that hunting for non-registered threads is more than most of us can manage. It doesn't look like an orphaned thread at first glance you see.
Mar 2 '07 #9
NeoPa
32,579 Recognized Expert Moderator MVP
I'm assuming that the control [FishControl] (You need to supply the name of this control) is displayed and updated when a species is selected in the ComboBox [Species].
If so then the following should work for you :
Expand|Select|Wrap|Line Numbers
  1. Private Sub FishControl_AfterUpdate()
  2.   Me![subfrm_FishSpecimen].Visible = Me!FishControl
  3. End Sub
Mar 2 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2357
by: CSDunn | last post by:
Hello, In Access ADP's that connect to SQL Server databases, any time I have a situation where I have a combo box in a main form that looks up a record in a subform, the subform record source has to be based on either a View or a Table. I can almost always use a View, and it helps to do this since I can have better control over the size of the RecordSet of the subform. There are times when the use of a Stored Procedure would give me...
1
3485
by: Donna Sabol | last post by:
I have a form (frm_MAIN_RPT) that contains a combo box (Combo6) & subform (dbo_REQ_subform). The combo box is used to select the "cost center" data that will be displayed in the subform. From there, I have a button that runs a report based on which item I select in the subform. 1. The Combo Box correctly displays the list of cost centers within the combo box. 2. The subform, when opened separately, correctly displays the data for the...
0
1159
by: misscrf | last post by:
I have this normalized database, and I don't understand why setting up the forms is so hard for me. I have the main candidate entry form. This is to enter in candidates who apply for a job. I have a tab control with a page that shows if the candidate type is 1 of a few types ( in code). Otherwise 2 pages show. 1 is for contact information. This has an address subform and a phone subform. I can't figure out how to show all address,...
1
2155
by: NBruch | last post by:
Ok let me explain what im trying to do: i need a combo box which needs to be linked to a listbox (or combo box doesnt matter which really) then the listbox should bring up the record in a subform. so you pick a name (in the combo box in the main form) then it should bring up all the dates for which the person has a record for (in the list box or combo box) and then once you pick a date it should bring up the specific record you want...
1
2295
by: MLH | last post by:
I have a form with a subform control on it listing records returned by a saved query named UnbilledVehicles. I would like to put command buttons on the main form to apply dynamic filters to the records displayed in the subform control. Say, for instance, to list only 2004 model cars. I'm seeking the simplest approach. The main form is frmCreateInvoice, the subform control is named frmCreateInvoiceSubFormCtl and the actual sub- form is...
9
9717
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
2
2017
by: Rex | last post by:
I have a main form and a subform. The main form has a combo box control and the subform has a textfield. What I want to do is when the main form loads it should check the value of the combo box and if it, lets say "MZ" then the textfield in the subform must be enabled otherwise it should be remain disabled. This should be done without setting any explicit focus. Any help would be greatly appreciated.
4
3433
by: JHite | last post by:
I am using Access 2003 on Windows XP. This is a simple database that contains “tblStaffers” containing names of the office staffers, “tblProjects” containing names of the office projects, and “tblStatusReports” containing the “StafferID” (link to Staffer table), the “ProjectID” (link to Projects table), a “Date” field, and a “Notes” field. There is a main form, “frmStaffEntry,” with a subform, “frmStatusReports,” for a user to...
5
2623
by: JHite | last post by:
I’m using Access 2003 on Windows XP. I have a Mainform (Staff Entry) which displays an unbound combo box named ChooseStaff. The Row Source for this combo box is a SELECT of the UserIDs and Staffer Names from a table (Staffers) which contains staffers’ names and other info. When the user actually selects a Staffer Name from the dropdown combo box, the VBA routines for the combo box’s On Enter and After Update events take care of finding and...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.