Results 1 to 8 of 8

Thread: Passing a variable from form to form.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Passing a variable from form to form.

    I have two identical forms (design). One populates a recordset (substances) into a listview, the other populates another recordset (workstations) into a listview. But both of these forms are identical design wise. I want to have just one form to display both recordsets, by means of a flag on form show or load (i'm not sure how). In the form is a sub PopulateListview:



    Public Sub PopulateListview(loadRecordset As Integer)

    Dim SQL As String
    Dim lsiListItem As ListItem
    Dim rs As ADODB.Recordset


    Set rs = New ADODB.Recordset

    Select Case loadRecordset

    Case 0


    SQL = "SELECT SubstanceAssess.SubstanceAssessID, SubstanceAssess.CoshhRef, SubstanceAssess.SubstanceName, User.Description FROM [User] INNER JOIN SubstanceAssess ON User.UserID = SubstanceAssess.UserID WHERE (((SubstanceAssess.Archived)=-1))"


    Case 1


    SQL = "SELECT WorkstationAssessID, WorkstationName, Location, User.Description FROM [User] INNER JOIN WorkstationAssess ON User.UserID = WorkstationAssess.UserID WHERE expired=True order by WorkstationName"


    End Select



    lsvListView.ListItems.Clear

    With rs
    .Open SQL, conn, adOpenKeyset, adLockOptimistic
    If .RecordCount = 0 Then Exit Sub

    Do Until .EOF
    Set lsiListItem = lsvListView.ListItems.Add(, , rs(0))
    lsiListItem.SubItems(1) = rs(1)
    lsiListItem.SubItems(2) = rs(2)
    lsiListItem.SubItems(3) = rs(3)
    .MoveNext
    Loop
    .Close
    End With

    lsvListView.SelectedItem = Nothing

    Set rs = Nothing

    End Sub

    This sub works when I set loadRecorset in form load. However the form will be loaded from a MDI form. So I need to pass loadRecordset to the form from the MDI. How? The only parameters when loading a form is modal?

    Thanks.

    Daz

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Load the form by simply using Load Form1. And then call the PopulateListView method such as:

    Form1.PopulateListview 0

    or 1 whatever you like. Make sure the PopulateListview method in the form remains public, else you may not be able to call it from outside.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    You can put your LoadListView procedure in a .BAS module, make it Public, and pass it the Form Name, ListView name and Recordset name when you call it. Add the parameters in your procedures declaration
    VB Code:
    1. Public Sub PopulateListView(MyForm As Form, MyList As ListView, MyRs As ADODB.Recordset)
    In this manner, you could use it from anywhere in your project. Example:
    VB Code:
    1. Call PopulateListView(Me, ListView1, Rs)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169
    Thanks for the help guys. One more question. If a recordset has more fields than another recordet, how can the listview in the PopulateListview sub set only the required amount of listitems. For example:

    Do Until .EOF
    Set lsiListItem = MyList.ListItems.Add(, , rs(0))
    lsiListItem.SubItems(1) = rs(1)
    lsiListItem.SubItems(2) = rs(2)
    lsiListItem.SubItems(3) = rs(3)
    .MoveNext
    Loop

    This is ok if the rs has 4 fields, but if the recordset has 5 fields how does the listview know this?

    Thanks again.

    Daz
    Last edited by seh; Sep 28th, 2002 at 11:50 AM.

  5. #5
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Add more subitems I guess

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    You are creating the recordset, correct? Adjust your Sub items accordingly...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  7. #7
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Just feeling pretty bored, so here is some code for ya:

    Code:
    Dim I As Integer
    For I = 1 To rs.Fields.Count - 1 Step 1
        ListView.SubItems(I) = rs.Fields(I)
    Next
    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Location
    Coventry, England
    Posts
    169

    Re: Well ...

    Originally posted by honeybee
    Just feeling pretty bored, so here is some code for ya:

    Code:
    Dim I As Integer
    For I = 1 To rs.Fields.Count - 1 Step 1
        ListView.SubItems(I) = rs.Fields(I)
    Next
    .
    Thanks honeybee. I came up with this bit of code very similar to yours last night (intoxicated as it were).

    With rs
    If .RecordCount = 0 Then Exit Sub
    Do Until .EOF
    Set lsiListItem = MyList.ListItems.Add(, , rs(0))
    For i = 1 To rs.Fields.Count - 1 Step 1
    lsiListItem.SubItems(i) = rs(i)
    Next
    .MoveNext
    Loop
    End With

    I found that I had to set the listitem before I did the subitem stepthrough.

    Oh well it works.

    Thanks all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width