473,395 Members | 2,010 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,395 software developers and data experts.

Access 2007 Using DAO to Class to db Tables

I've avoided using classes in Access for years, but now I have a need to create a class. The application I'm writing is to track sheriff dept fleet (automobiles). I've created a class for Equipment (radar, radio, gun racks, etc.) with the properties for vehicle id, equipment id (identifies type of equipment), model, serial number and notes. Then I think I need a collection to hold all the equipment that could be on one sheriff car. I'm working on the add new vehicle piece. So once the user has selected (equipment type) from a dropdown and entered model, sn, etc. Click "Add" it goes in the collection. Do I write the DAO in the class itself to update the VehicleEquipment table? Sorry for the lengthiness of this post. I'll post the class I've created so far... Thanks in advance for your help.
Attached Files
File Type: txt EquipmentClass.txt (1.2 KB, 508 views)
Mar 1 '11 #1

✓ answered by NeoPa

It appears that you have come from a programming background and have little experience with database concepts. How can I say that without sounding dismissive? I expect I should just add that my intention is to help here, and I need to understand where you're coming from in order to do that effectively. Apologies for any disrespect inferred. None was implied. I would add that your proposed solution, while not very practical in a database environment, is actually quite a clever one, given the paradigm I assume you are working within.

In a database situation, a collection would be held in a table. Linking the data in the table to wherever it is relevant in your structure is done with Primary & Foreign Indexes. In a situation like this I would expect there to be a table that contained details of all the cars; another table to contain the possible types of equipment available to the cars; another table, with Foreign Indexes to both of the two prior tables, that contains the individual pieces of equipment, such as a short-wave radio in sherrif Bob's new car.

I appreciate that this is a bit of a paradigm-shift for you, but does that sound like it makes sense?

In case it helps, you may want to check out Database Normalisation and Table structures. This gives a bunch of tips on how tables should best be designed to handle the requirements you have of them.

14 2483
TheSmileyCoder
2,322 Expert Mod 2GB
I dont understand why you need to create a class for this? Why not simply store it in a table and relate it to your main table?
Mar 2 '11 #2
Because I want to allow the user to add several pieces of equipment to a vehicle, but not commit the data to a table until the entire vehicle is saved. If I save to a table while they're adding equipment to a new vehicle, they may cancel the vehicle creation. I am very open to ideas.
My current concept of the events and system responses: 1) User enters vehicle data (VIN, Yr, Make, Model, etc.) 2) Adds equipment using a listbox to choose type of equipment and textboxes for entering identifying information about the equipment (model, serial number... if any) 3) User clicks Add Equipment; equipment class object is created and added to a collection (which I'm also having trouble getting to work) 4) Iterate through collection and display the equipment added so far on the form (using subform or textboxes?) 5) User clicks Save Vehicle and iterate through the collection saving the equipment to database.
I guess my question is just: Do I write the DAO in the class itself to save the collection or is that written behind the form?
Mar 2 '11 #3
TheSmileyCoder
2,322 Expert Mod 2GB
I honestly have no experience in the method your propossing. I can't say whether it is feasable or not. I think however if it was me, I would prefer using temporary tables, to store the information, before commiting it. I think thats the easier approach (that said, the easiest approach is not always the right approach, thats up to you).
Mar 7 '11 #4
NeoPa
32,556 Expert Mod 16PB
It appears that you have come from a programming background and have little experience with database concepts. How can I say that without sounding dismissive? I expect I should just add that my intention is to help here, and I need to understand where you're coming from in order to do that effectively. Apologies for any disrespect inferred. None was implied. I would add that your proposed solution, while not very practical in a database environment, is actually quite a clever one, given the paradigm I assume you are working within.

In a database situation, a collection would be held in a table. Linking the data in the table to wherever it is relevant in your structure is done with Primary & Foreign Indexes. In a situation like this I would expect there to be a table that contained details of all the cars; another table to contain the possible types of equipment available to the cars; another table, with Foreign Indexes to both of the two prior tables, that contains the individual pieces of equipment, such as a short-wave radio in sherrif Bob's new car.

I appreciate that this is a bit of a paradigm-shift for you, but does that sound like it makes sense?

In case it helps, you may want to check out Database Normalisation and Table structures. This gives a bunch of tips on how tables should best be designed to handle the requirements you have of them.
Mar 7 '11 #5
patjones
931 Expert 512MB
Hi,

It's completely possible to implement classes in VBA, but I have personally found that approach to be more laborious than other standard methods available in Access, as Smiley alludes to. Also bear in mind that one of the motivations for object oriented programming is portability of the classes. If this is a very specialized application then your classes may not have much relevance in other projects.

As NeoPa suggests, the way we usually like to go is build a normalized table structure (relational database). For the user interface, there are various things you can do, but it seems like an unbound form will suit your needs well. This is where you enter all the information for a new record and then complete an action to commit all the information to the tables, such as hitting a command button that you put on the form. The nice thing about this approach is that the user can go back and change information prior to committing it. You can even put another button on the form that allows them to clear it altogether and start over.

Let us know if you might be interested in going this way, and I'm sure we can give you further guidance.

Pat
Mar 8 '11 #6
Thank you for your response. I've never used classes in Access before and was unsure how to save the data. I must be very confused because the responses here seem to indicate that I'm way off base in my concept of class usage. I could have used a temporary table to save the vehicle equipment selections, but I thought a class and collection would have a lower overhead. I believe I'm quite competent with database design and normalization and the database table that the vehicle equipment is being saved into relates back to the vehicle. I've decided to just iterate through the listbox on the form and save the equipment that has been added by the user in the process of creation the vehicle. Not sure what I've done or said wrong in this post, or maybe I'm just too sensitive, but I definitely feel that the responses have been quite condescending and critical. Sorry to have troubled you all. I'll close this post now.
Mar 8 '11 #7
patjones
931 Expert 512MB
Hi,

I can understand where we may come across that way, but - and I can only speak for myself - no such condescending attitude is intended.

In your situation, what I like to do (and I'm just laying out a general map) is set a recordset up, attach it to my tables, assign the control values to the recordset, and commit the data. There are numerous other ways.

Expand|Select|Wrap|Line Numbers
  1. Dim rs As DAO.Recordset
  2.  
  3. Set rs = CurrentDb.OpenRecorset("tblName", dbOpenDynaset)
  4.  
  5. rs.AddNew
  6.  
  7. rs.Field1 = textbox_value1
  8. rs.Field2 = textbox_value2
  9. rs.Field3 = combobox_value1
  10.  
  11. ...
  12.  
  13. rs.Update
  14.  
  15. Set rs = Nothing

Here, "tblName" is the name of the table you're putting the data into. If there are multiple tables, you can either go through this process with multiple recordsets, or stay with the one recordset and specify a query that encompasses all the joined tables.

rs.AddNew tells Access we're going to add a new record, while rs.Update actually commits the data. Even though the user put values in the various controls, nothing is stored in the database until rs.Update happens.

Again, there are other possibilities.

Pat
Mar 8 '11 #8
NeoPa
32,556 Expert Mod 16PB
I apologise if anything I posted upset you. I fully accept that my perspective is largely guesswork on my part, built simply from your posts. I'm happy to accept I may be way off base here, but that's how it appeared to me based on admittedly limited information. My intention was never to insult you. I had no idea that you considered yourself more experienced than I assumed (That's not supposed to sound condescending either - It's merely an illustration of the difficulties we face when dealing with people we don't know - a necessary part of contributing on a site like this.)

Personally, I feel that using session-based data (as is typically, though not necessarily, used in a class type structure) would not be appropriate for what I understand you want - hence I suggested an alternative approach. If any of my assumptions are wrong then I'm happy that the choice of what to do is still firmly in your hands. Even if all my assumptions are absolutely correct, it is still you that ends up making the choices (which is absolutely as it should be).

At the end of the day, I'm sure none of us (I've been rubbing shoulders with the others for some years now) had any intention of insulting you, or showing any lack of respect for your skills. If you feel though, that what I contributed was not what you want to hear, I'm happy to leave this thread in other very capable hands.

Good luck with your project.
Mar 8 '11 #9
Expand|Select|Wrap|Line Numbers
  1. * * * * * * * * * *
  2.  
  3. Dim newEq As CEquipment
  4. Dim iListEq As Integer
  5. Dim strListBox as String
  6.  
  7. If Me.lstEquip.Value = "" Then
  8.     iListEq = 0
  9. Else
  10.     iListEq = Nz(Me.lstEquip.Value, 0)
  11. End If
  12.  
  13. Set newEq = New CEquipment
  14.  
  15. If iListEq = 0 Then
  16.     MsgBox "Nothing to add.  Please select Equipment from List."
  17. Else
  18.     With newEq
  19.         .EQID = Me.lstEquip.Value
  20.         .Name = Me.lstEquip.Column(1)
  21.         .Model = Nz(Me.txtModel, "")
  22.         .Serial = Nz(Me.txtSN, "")
  23.         .Note = Nz(Me.txtNote, "")
  24.     End With
  25.  
  26.     strListBox = newEq.EQID & g_SEMI & newEq.Name & g_SEMI & newEq.Model & g_SEMI & newEq.Serial & g_SEMI & newEq.Note
  27.  
  28.     Me.lstMyVehEqp.AddItem strListBox
  29.  
  30.     'CLEAR SOURCE LISTBOX AND TEXTBOXES
  31.     clearEquipBoxes
  32.  
  33. End If
  34.  
  35. End Sub
  36.  
  37. * * * * * * * * * * *
  38.  
  39. 'AND THE PERTINENT PART OF THE VEHICLE_SAVE EVENT
  40.  
  41.    'STARTS AT 1 BECAUSE FIRST ROW IS HEADING
  42.     Select Case Me.lstMyVehEqp.ListCount
  43.         Case 1
  44.             bHasEquip = False
  45.         Case Is > 1
  46.             bHasEquip = True
  47.     End Select
  48.  
  49.     If bHasEquip Then
  50.  
  51.         Set rstEquip = dbsFleet.OpenRecordset("VehicleEquip_test", dbOpenDynaset)
  52.  
  53.         With rstEquip
  54.  
  55.             For i = 1 To Me.lstMyVehEqp.ListCount - 1
  56.                 .AddNew
  57.                 .Fields("V_ID") = Me.V_ID
  58.                 .Fields("EQ_ID") = Me.lstMyVehEqp.Column(0, i)
  59.                 .Fields("E_Model") = Me.lstMyVehEqp.Column(2, i)
  60.                 .Fields("E_SN") = Me.lstMyVehEqp.Column(3, i)
  61.                 .Fields("E_Notes") = Me.lstMyVehEqp.Column(4, i)
  62.                 .Update
  63.             Next i
  64.  
  65.         End With
  66.             rstEquip.Close
  67.             Set rstEquip = Nothing
  68.     End If
  69.  
  70. * * * * * * * * * * * * *
  71.  
  72. 'THE CLASS MODULE....
  73. Option Compare Database
  74. 'WHEN ADDING A NEW VEHICLE THERE WILL NOT BE A V_ID UNTIL THE VEHICLE IS SAVED FOR THE FIRST TIME
  75. 'BUT I'LL NEED THE VID WHEN IT COMES TO EDITING
  76. Private pVID As Double
  77. Private pEQID As Integer
  78. Private pEQname As String
  79. Private pEQnote As String
  80. Private pEQmodel As String
  81. Private pEQserial As String
  82. Public Property Get VID() As Double
  83.     VID = pVID
  84. End Property
  85. Public Property Let VID(Value As Double)
  86.     pVID = Value
  87. End Property
  88. Public Property Get EQID() As Integer
  89.     EQID = pEQID
  90. End Property
  91. Public Property Let EQID(Value As Integer)
  92.     pEQID = Value
  93. End Property
  94. Public Property Get Name() As String
  95.     Name = pEQname
  96. End Property
  97. Public Property Let Name(Value As String)
  98.     pEQname = Value
  99. End Property
  100. Public Property Get Model() As String
  101.     Model = pEQmodel
  102. End Property
  103. Public Property Let Model(Value As String)
  104.     pEQmodel = Value
  105. End Property
  106. Public Property Get Serial() As String
  107.     Serial = pEQserial
  108. End Property
  109. Public Property Let Serial(Value As String)
  110.     pEQserial = Value
  111. End Property
  112. Public Property Get Note() As String
  113.     Note = pEQnote
  114. End Property
  115. Public Property Let Note(Value As String)
  116.     pEQnote = Value
  117. End Property
Mar 9 '11 #10
Thank you all for being a sounding board. I apologize if my frustration caused me to lash out, but I do think we all need to be careful making assumptions... and perhaps focusing on the question being asked rather than re-writing someone else's solution. I may not write code in the best and most elegant fashion, but I get the job done. Peace out.
Mar 9 '11 #11
NeoPa
32,556 Expert Mod 16PB
NaughtyZute:
Peace out.
That means "I've had my say but the subject's closed now." doesn't it :-D I'm just messing with you of course. I don't believe anyone was too upset by your earlier comments. I'm normally quite strict with members that get too shirty on here, and I didn't feel you'd crossed any line.

I'm afraid that expecting people to help you without making any assumptions though, is just not going to happen except for the very simplest of questions. In a perfect world where all required information is passed in the original post, this may be possible. Otherwise we often have little but assumptions to go on. None of us could have been much help here without making assumptions. Many of which were not even discussed or noticed. It's the nature of the beast. You should appreciate that answering technical questions about a project that we don't even have in front of us is not the easiest thing to manage. We have to fill in the gaps by using our experience. There is no other practicable way to work in the circumstances.
Mar 10 '11 #12
I didn't mean it that way. My only intention was to show what I ended up doing in case it will help someone else. I actually feel like crying right now because I feel so misunderstood. This is why I normally just google the thing until I find someone else's question answered. I'll make sure to do that in the future. My big feet just seem to step all over everything. Now I am crying so... I'm definitely not suited to this kind of back and forth. Sorry again that I offended anyone and I'm sorry I ever asked a question. Maybe it's not too late to become a line cook at McDonald's.
Mar 10 '11 #13
NeoPa
32,556 Expert Mod 16PB
I understand you. At the risk of making myself further misunderstood, I really wasn't trying to have a go at you.

I understood that your posting your code was just showing the solution you came up with that you were happiest with. It wouldn't be the approach I would take, but that's really very unimportant. What's important is that it suits you. We, as a site, and I am in a position to speak for the site, are happiest when members do feed back their solutions, especially in those cases where they don't feel what was offered was quite what they were after. No criticism is inferred in cases such as these. We understand that the internet is a very big place and there will always be people who see things differently. You weren't rude or impolite in expressing your views. Of course there's room here for that.

I hope you don't go away feeling sorry you asked the question. I know many people who don't feel comfortable with too much back and forth, but that isn't a sign they need to consider cooking for McDonalds. I have to say, even if I didn't agree with your approach, I always felt you were reasonably clever to handle it in the first place. Not everyone, even in the software industry, is even comfortable working with classes at all.
Mar 10 '11 #14
Thank you. That means a lot to me. In retrospect, I think it was this line tht got us off on the wrong foot and painted my attitude for any future postings: It appears that you have come from a programming background and have little experience with database concepts. I appreciate your help. I am always striving to be a better programmer, as I'm sure we all are. It is such a subjective activity -- but that's what makes it fun. Thanks again.
Mar 11 '11 #15

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

Similar topics

2
by: moses40 | last post by:
I need help importing an excel spreadsheet into an existing access table using vba. However I have a few requirements. 1. It needs to prompt the user to specify where the spreadsheet is 2. It needs...
1
by: AccessHunter | last post by:
Hi, I am trying to create a new application to work with my SQL server database from my Access 2007 using the upsize wizard. It gave me an error saying, "The upsizing wizard cannot convert your...
1
by: transpac80 | last post by:
Problem with SQLPassthrough in Access 2007 using DAO I have this piece of VBA code from inside Access to retrieve data from external database (SQL or Oracle) sConn =...
5
by: sphinney | last post by:
I have data in a few external Access tables that I want to link to my Access 2007 database. Depending on situation (and permissions of the person using the database) I want to dynamically link to...
0
by: ABinBoston | last post by:
Is it possible to change the AutoFormat in Access 2007 using code. There are a number of named auto format themes - like Apex, Aspect, Civic, etc... I would like to have an Access database,...
0
by: ARC | last post by:
Hello all, For right-click (shortcut) menus in access 2007, I've been using a round-about method of opening access 97 on an old computer, modifying my own custom shortcut menus, then importing...
1
by: kfry | last post by:
I am having an issue maybe someone can help with. I have written an application with Access 2003 on my XP pc. I then package it and install it on my VISTA Access 2007 PC. When I do this the wrong...
3
by: VivDenham | last post by:
Hi there In MS Access 2007, I have a Form which contains fields that are Visible = False in certain circumstances, and this works perfectly. When I use this form as a Subform in another form,...
1
by: L Chapman | last post by:
I have a form "form2" witha combobox called "namesearch" that lists names from the table "students" once a student is selected from the combobox i want to use a button to only show reports of that...
0
by: izharmel | last post by:
Hi, I import a csv file to an access table using the following code as a part of a larger code: DoCmd.TransferText acImportDelim, , strTableName, strPath & objFile.Name, True Prior to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.