Results 1 to 12 of 12
  1. #1
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8

    Question Number of rows in a subform by combo box


    Ok, I'll try to explain this as best as I can... bear with me!

    This is the situation. I have a series of drawings (identified by a drawing number) and each drawing can have a number of bars across it. There will usually be between 0 and 9 bars on the drawing, but I can't rule out there being more. For each bar, there are other pieces of information, but thats not that relevant to my problem.

    So far: Drawings (by drawing number) with bars (0 to 9 usually).

    Now, I want to include a subform on the main drawings form displaying the information about the bars for that drawing.

    So far I have a table for the drawings, and a table for the bars, with a one-to-many relationship (one drawing, many bars) by DrawingNo.

    Ideally, what I would like is to be able to select the number of bars required for the drawing in a combo box, and then the correct number of rows appear in my subform. Similarly, if the number of bars is changed in the combo box, I'd like the number of rows in the subform to reflect this (deleting entries or adding new ones as required).

    So: select number of bars in combo box, correct number of rows appear in subform.

    I've got the combo box and subform set up ok, just not the functionality to get the right number of rows dependant on the combo box.

    I'd really appreciate any help on this, sorry for the long post!

    Thanks

    outhwaik

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You would have to do this with VB code but I have a couple of questions first.

    You mentioned adding and deleting rows, is there data associated with these rows?

    let's say you change the number of rows from 9 to 4, how are you determining which rows are being deleted? If there is additional information for each row you are entering you stand to delete information you need if you just perform a delete.

    Let's say you want to add 2 rows to an existing 4 row drawing, do those two rows necessarily get tacked on at the end or is there going to be a need to insert records prior to the end of the existing records?

    I'm asking because I am always very hesitant to delete items from my databases because once they are gone, they are gone for good with no hope of recovery unless you have daily/hourly backups. I usually add a 'void' type option then exclude any record with the void checked off.

  3. #3
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8
    Pretty much the only time rows will be added/deleted is when the record is first created - the number of bars on a drawing will not change once added to the database, so I'm not worried about losing information.

    One of the pieces of information for a bar will be position, eg. if there are 4 bars, the bars will have position 1, 2, 3 and 4. If the number of bars was then changed to 2, positions 3 and 4 would be deleted. Similarly, if changing from 4 to 9 bars, positions 5 to 9 would be added on the end.

    I'm fine with using VB, I've been learning as I go along and I've understood all of the stuff I've done so far

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Let's assume you have these tables:

    Code:
    Tbl_Maps
    MapID MapName MapLines
    1     Map A   9
    2     Map B   7
    3     Map C   5
    
    Tbl_MapLines
    LineID (autonumber primary key)
    MapID (inherited from tbl_maps)
    LineNumber (starts at 1, ends at maplines)
    The code you would use (either by creating a module, addiing it to the ON CLICK EVENT of a button, etc) is this

    Code:
    dim db as database
    dim sSQL as string
    dim rst as recordset
    dim iMapID as long
    dim iTotalLines as integer
    dim iCurrLine as integer
    
    set db = currentdb
    ssql = "SELECT * FROM Tbl_Maps" 
    'you may enter a criteria if you are doing this for one map at a time 
    'when running this code you also want to make sure you are only running 
    'for maps that have no lines generated in the Tbl_MapLines table
    set rst = db.openrecordset ssql
    
    iCurrline = 1
    do while rst.eof <> true
        iMapID = rst.fields("MapID")
             iTotalLines = rst.fields("maplines")
        for  i = 1 to itotallines
            ssql = "INSERT INTO Tbl_MapLines (MapID, LineNumber) VALUES (imapid, i)
            db.execute ssql
            i = i + 1
        next i
        rst.movenext
    loop
    rst.close
    set db = nothing
    NOTE: I didn't test this fully but if I've missed something post back

  5. #5
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8
    Sorry I've not posted back until now - I've been away and haven't had chance to test the code out until now!

    I've copied the code across to run when a button is pressed, however the line
    Code:
    set rst = db.OpenRecordset ssql
    throws up a syntax error when it trys to compile. It's probably just a small alteration that's needed, but using the Database and Recordset types isn't something I'm familiar with, so I'm not sure what needs adjusting!

    Hope you can help me out - I know it's been a while since you looked at this thread!

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You have to substitute in your table name, my test table was tbl_Maps you have to put in your own table/field/database names to make the code work.

  7. #7
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8
    I've created a completely separate database to run this example in, so I don't get confused for exactly that reason! All names etc. are exactly as written in your example.

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Check the references in each database then, from your VBA window it's tools>references. Look at the ones I'm using vs the ones that are in your version of the database and add references until you find the right one.

  9. #9
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8
    Ok, the references I've got already are Visual Basic for Applications, Microsoft Access 11.0 Object Library, OLE Automation, Microsoft DAO 3.6 Object Library, and Microsoft ActiveX Data Objects 2.1 Library. I've tried just going down and adding them until the line stops causing a problem, but adding them all just causes name conflicts. Have you got the example to hand, and could tell me what other references are needed?

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Add only 1 reference at a time not the whole bunch and see if the code will run I don't know off the top of my head which ones you needs specifically. You usually only get the conflict between ADO and DAO references (at least the ones I've run in to). Alternately you can enable only the references I have see that it runs then start adding in the ones that you currently have to see which one causes the conflict.

  11. #11
    outhwaik is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by rpeare View Post
    Alternately you can enable only the references I have see that it runs
    Where have you put which ones you're using? Sorry if I'm being a bit slow here!

  12. #12
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Same place I mentioned before, open your VBA scripting window, go to tools> references it should list all the references you're using at the top of the window.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 01-02-2011, 10:09 AM
  2. Insert x number of rows based on value
    By opopanax666 in forum Programming
    Replies: 4
    Last Post: 10-26-2010, 03:26 AM
  3. creation of reports with fixed number of rows
    By georgia in forum Reports
    Replies: 1
    Last Post: 02-17-2010, 10:40 PM
  4. Replies: 0
    Last Post: 08-17-2008, 12:19 PM
  5. Replies: 1
    Last Post: 05-29-2008, 04:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums