473,408 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Using Multiple Checkboxes in a Normalized Database to add new records

Hello All, (using Access2000)

I have a form with multiple unbound checkboxes. What I would like to do is have the user check whoever needs to take a specific training course. My database is normalized, I have no Yes/no fields in my tables, what I have is VB code that inputs an IDnumber into the field that matches the specific job title for the employee that needs to take that course:

Expand|Select|Wrap|Line Numbers
  1. Private Sub chkMFGAssoc_Click()
  2.  If Me.chkMFGAssoc = -1 Then
  3.  Me.GroupID.Value = "0015"
  4.  Else
  5.  Me.GroupID.Value = ""
  6.  End If
  7. End Sub
This works great, it adds a new record in the join table (called "tblClassGroup") with the classID and the GroupID. My problem is that most of the time one class is trained to multiple Groups. I need to be able to click on multiple checkboxes and create multiple records using a single form and vb code (if possible!)

Some info:
tblGroups has GroupID feild GroupName Field
tblClass has ClassID, ClassName, Description, Trainer
Join - tblClassGroup has ClassGroupID, ClassID, GroupID

Thank you
V
Aug 2 '10 #1
12 2800
beacon
579 512MB
Hi V,

Have you looked into using the INSERT INTO SQL statement in association with the DoCmd.RunSQL command? This will create a new row in your table and will insert the values that you indicate into the appropriate fields.
Aug 2 '10 #2
thelonelyghost
109 100+
Like what @beacon was saying, you could run an INSERT INTO statement to create a new record. I'm not exactly sure how your tables are formed, but one use of this code is made safer by making a submit button, then coding it like so:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Submit_Click()
  2. Dim strSQL As String
  3.  
  4. If Me.chkMFGAssoc = -1 Then
  5.   strSQL = "INSERT INTO [tblMFGAssocClass] ([GroupID], [FieldName2], [FieldName3]) VALUES (" & Me.GroupID.Value & ", " & Me.ControlName2.Value & ", " & Me.ControlName3.Value & ");"
  6.  
  7.   DoCmd.RunSQL strSQL
  8. End If
  9. If Me.chkOtherCheckbox = -1 Then
  10.   strSQL = ...
  11.   DoCmd.RunSQL strSQL
  12. End If
  13.  
  14. ...
  15.  
You'll have to add a ' before and after each control value if it's a string, or a # if it's a date, but other than that it should work. Just repeat the If-Then statements for each checkbox while adapting the SQL code for each rule and you should be fine.

Alternately you can use CurrentDb.Execute instead of DoCmd.RunSQL (see THIS LINK) or (if need be) execute an UPDATE query instead of INSERT INTO, but that's up to you to figure out its uses. (n_n)
Aug 3 '10 #3
Thank you both for you help. I am attempting to enter the code now, but got lost at:

(" & Me.GroupID.Value & ", " & Me.ControlName2.Value & ", " & Me.ControlName3.Value & ");"

The table is being updates is called

tblClassGroup
ClassGroupID (primarykey)
ClassID
GroupID

the checkmarks need to represent the groupID field.

Thanks again
Aug 3 '10 #4
Steven Kogan
107 Expert 100+
If ClassGroupID is an autonumber field, you do not need to specify it in the SQL. If ClassID is stored in the ClassID field, line 5 might be:

strSQL = "INSERT INTO [tblClassGroup] ([GroupID], [ClassID]) VALUES (15, " & Me.ClassID.Value & ");"
Aug 3 '10 #5
I think i got it, but when i attempted to test, i got an syntax error

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEnter_Click()
  2. Dim strSQL As String
  3.  
  4. If Me.chkHRRep = -1 Then
  5.     strSQL = "INSERT INTO tblClassGroup ([ClassGroupID], [ClassAreaStationID], [GroupID]) VALUES (" & Me.ClassGroupID.Value & ", " & Me.txtClassAreaStationID.Value & ", " & 19 & ")"
  6.  
  7.     DoCmd.RunSQL strSQL
  8. End If
  9.  
  10. End Sub
the error is in the DoCmd.RunSQL strSQL line
Aug 3 '10 #6
Steven Kogan
107 Expert 100+
What is the error message displayed?

If ClassGroupID is an autonumber field then you should not be able to specify it in you INSERT INTO sql.
Aug 3 '10 #7
beacon
579 512MB
Is Me.txtClassAreaStationID.Value a text field? If so, you need to add single quotes on the inside of the double quotes surrounding the field in the SQL string.

It will look like this:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblClassGroup ([ClassGroupID], [ClassAreaStationID], [GroupID]) " & _
  2.          "VALUES (" & Me.ClassGroupID.Value & ", '" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
  3.  
Notice the single quotes: VALUES (" & Me.ClassGroupID.Value & ", '" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
Aug 3 '10 #8
Trying it without the primary key field noted

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEnter_Click()
  2. Dim strSQL As String
  3.  
  4. strSQL = "INSERT INTO tblClassGroup (ClassAreaStationID, GroupID) " & _
  5.          "VALUES ('" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
  6.  
  7.     DoCmd.RunSQL strSQL
  8.  
  9. End Sub
I get an error saying it can't append do to key violations.

trying it with the primary key as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEnter_Click()
  2. Dim strSQL As String
  3.  
  4. strSQL = "INSERT INTO tblClassGroup (ClassGroupID, ClassAreaStationID, GroupID) " & _
  5.          "VALUES (" & ClassGroupID.Value & ", '" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
  6.  
  7.     DoCmd.RunSQL strSQL
  8.  
  9. End Sub
Also get the same error that I can not append due to key violations

Thank you guys so much for your help!!
Aug 4 '10 #9
thelonelyghost
109 100+
Three things:
  1. Check the datatype of ClassGroupID
  2. What are the fields, tables, and datatypes?
  3. Syntax issues
(1)
To start, I apologize because I misunderstood the situation with your tables. I thought ClassID, GroupID, and ClassGroupID were all different tables. Since you said that ClassGroupID is the primary key, are you positive it's an autonumber datatype? If not, that may be your issue. If it is, @Steven Kogan is right: you shouldn't need to even address that field in strSQL.

(2)
Additionally, I'm confused as to what the fields are in your table. I see that you said they are ClassID, GroupID, and ClassGroupID (Primary Key) in post 4 but then you bring up fields such as ClassAreaStationID... are there more fields affected your forgot to tell us about?

(3)
One thing I see wrong in the following code (syntactically speaking) is the use of "', " & 19 & ")". If it's a constant value then there's no need to concatenate like that. The use of & in this sort of case is mostly so that the code can be dynamic to the form's values. Just write it as "', 19)" and that may solve your issue. (see post 11)
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblClassGroup (ClassAreaStationID, GroupID) " & _
  2. "VALUES ('" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblClassGroup (ClassGroupID, ClassAreaStationID, GroupID) " & _
  2. "VALUES (" & ClassGroupID.Value & ", '" & Me.txtClassAreaStationID.Value & "', " & 19 & ")"
Aug 4 '10 #10
thelonelyghost
109 100+
Also, I figured I should clarify what "(" & Me.ControlName.Value & ", " is supposed to mean. As you know, you can define a string like so: strString1 = "Hello World!". To make the value dynamic, you can create a form that has you input a value in the string, i.e. strString2 = "Hello " & Me.txtName.Value & "!". The title of the form's input (control) in this case is "txtName".

Let's say I input the name "Tom" into the form. If you printed the value of strString1 and strString2 after they've been set, you would see
Expand|Select|Wrap|Line Numbers
  1. strString1:
  2. "Hello World!"
  3.  
  4. strString2:
  5. "Hello Tom!"
Did that help clear things up in post 4?
Aug 4 '10 #11
I finally got this working. I was getting a key violation and after doing some research, found that if i took off the "referential integrity" it would work.. and it did.

(NOte: I made a bunch of changes to my tbl structure for another reason, so the names are different)

Thank you so much for you help!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEnter_Click()
  2.     Dim strSQL As String
  3.  
  4. If Me.chkHRAdmin = -1 Then
  5.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  6.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0008')"
  7.  
  8.     DoCmd.RunSQL strSQL
  9. End If
  10.  
  11. If Me.chkHRRep = -1 Then
  12.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  13.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0002')"
  14.  
  15.     DoCmd.RunSQL strSQL
  16. End If
  17.  
  18. If Me.chkMatBuy = -1 Then
  19.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  20.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0004')"
  21.  
  22.     DoCmd.RunSQL strSQL
  23. End If
  24.  
  25. If Me.chkMatIA = -1 Then
  26.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  27.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0010')"
  28.  
  29.     DoCmd.RunSQL strSQL
  30. End If
  31.  
  32. If Me.chkMatLead = -1 Then
  33.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  34.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0015')"
  35.  
  36.     DoCmd.RunSQL strSQL
  37. End If
  38.  
  39. If Me.chkMatShip = -1 Then
  40.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  41.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0021'')"
  42.  
  43.     DoCmd.RunSQL strSQL
  44. End If
  45.  
  46. If Me.chkPDCAD = -1 Then
  47.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  48.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0005')"
  49.  
  50.     DoCmd.RunSQL strSQL
  51. End If
  52.  
  53. If Me.chkPDENG = -1 Then
  54.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  55.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0026')"
  56.  
  57.     DoCmd.RunSQL strSQL
  58. End If
  59.  
  60. If Me.chkPDLead = -1 Then
  61.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  62.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0017')"
  63.  
  64.     DoCmd.RunSQL strSQL
  65. End If
  66.  
  67. If Me.chkPDTech = -1 Then
  68.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  69.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0007')"
  70.  
  71.     DoCmd.RunSQL strSQL
  72. End If
  73.  
  74. If Me.chkMFGAssoc = -1 Then
  75.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  76.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0011')"
  77.  
  78.     DoCmd.RunSQL strSQL
  79. End If
  80.  
  81. If Me.chkMFGTech = -1 Then
  82.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  83.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0014')"
  84.  
  85.     DoCmd.RunSQL strSQL
  86. End If
  87.  
  88. If Me.chkMFGENG = -1 Then
  89.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  90.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0012')"
  91.  
  92.     DoCmd.RunSQL strSQL
  93. End If
  94.  
  95. If Me.chkMFGMgr = -1 Then
  96.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  97.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0013')"
  98.  
  99.     DoCmd.RunSQL strSQL
  100. End If
  101.  
  102. If Me.chkQAAdmin = -1 Then
  103.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  104.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0001')"
  105.  
  106.     DoCmd.RunSQL strSQL
  107. End If
  108.  
  109. If Me.chkQALead = -1 Then
  110.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  111.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0020')"
  112.  
  113.     DoCmd.RunSQL strSQL
  114. End If
  115.  
  116. If Me.chkQAPI = -1 Then
  117.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  118.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0018')"
  119.  
  120.     DoCmd.RunSQL strSQL
  121. End If
  122.  
  123. If Me.chkQAIA = -1 Then
  124.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  125.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0009')"
  126.  
  127.     DoCmd.RunSQL strSQL
  128. End If
  129.  
  130. If Me.chkOPSIT = -1 Then
  131.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  132.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0022')"
  133.  
  134.     DoCmd.RunSQL strSQL
  135. End If
  136.  
  137. If Me.chkPlantMgr = -1 Then
  138.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  139.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0016')"
  140.  
  141.     DoCmd.RunSQL strSQL
  142. End If
  143.  
  144. If Me.chkOPSSup = -1 Then
  145.     strSQL = "INSERT INTO tblClassAreaStation (CategoryID, ClassID, AreaID, StationID, JobTitleID)" & _
  146.     "VALUES ('" & Me.cboCategory.Value & "', '" & Me.txtClassAreaStationClassID.Value & "', '" & Me.cboArea.Value & "', '" & Me.cboStation.Value & "', '0025')"
  147.  
  148.     DoCmd.RunSQL strSQL
  149. End If
  150.  
  151. End Sub
My last question for this would just be how do i turn off the warnings so the user doesn't get "you are about to append..." message for every record that is being appended?

~BL
Aug 9 '10 #12
NeoPa
32,556 Expert Mod 16PB
One way is to use SetWarnings.
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.SetWarnings(False)
  2. Call DoCmd.RunSQL(strSQL)
  3. Call DoCmd.SetWarnings(True)
The other is to use :
Expand|Select|Wrap|Line Numbers
  1. Call CurrentDB.Execute(strSQL)
The Execute method doesn't display the messages. Use the dbFailOnError option to handle items that are locked or otherwise unable to be effected by the query.
Aug 9 '10 #13

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

Similar topics

0
by: Faybert | last post by:
Hello, and Thanks in advance for any light you might shed on my troubles. I'm trying to setup a series of checkboxes, or a checkboxlist to control the results that are shown on a gridview...
2
by: Patrick | last post by:
Hello, I need some advice on how to handle the following scenario in Javascript. Imagine an HTML page that displays relational database records from an address book application. Each record...
6
by: jeffsnox | last post by:
Hi, I have multiple checkboxes on the same form as follows: <input type='checkbox' name='cbtype' value='1'> <input type='checkbox' name='cbtype' value='2'> <input type='checkbox'...
0
by: joebob8000 | last post by:
This seems like a simple task, but my 6 year old roots in classic ASP must be causing me trouble with my current problem. I am looking to provide a search for users in which they can select...
4
by: ramapv | last post by:
can i highlight a checkbox from a group of checkbox with particular name which is given as a search key. I am having a list of checkboxes and i have to select some of them and form a group.but i'm...
4
by: favor08 | last post by:
I have a continious subform that 9000 + records and I have serveral check marks on the subform. so it ties to that record. Does anyone have any examples were they use multiple checkboxes in a ...
0
by: serghei | last post by:
I have already the insert form and the database. It's working to insert multiple data with multiple checkboxes. But I can't retrieve multiple data.
3
by: santoshjsh | last post by:
hello everyone, i have a gridview in which i have multiple checkboxes in a single column. means for every row in the gridview i have four checkboxes in one column e.g Add, Delete, Print,...
3
by: raamay | last post by:
hey experts, please advise me what is the best way to save multiple checkboxes value in a database. I have 6 checkboxes and i came across storing the values in a single column of a table which i dont...
0
by: kimmelsd33 | last post by:
I am adding to my software. I have placed about 30 checkboxes on a form. Based on which checkboxes are selected, I want to print the values to a tab delimited text file. For instance, the first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.