Results 1 to 28 of 28

Thread: [RESOLVED] Re-using a form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Resolved [RESOLVED] Re-using a form

    I have a form for registering new movies (frmMovies). On this form I have several comboboxes representing genre, actor, producer, director etc. On the side of each combo, there is a commnad button (cmdAdd) which enables the user to add more items into the respective tables that populate the comboboxes. Now, the form for adding the items (frmAddItem) has a ListView control which lists all the existing items in the respective table. Eg if I click on cmdAdd next to the actor combo, it should open frmAddItem form but in the listview, all the existing actors are listed. Should I click on cmdAdd next to producer, then frmAddItem's ListView should list all existing producers from the producer table etc.

    Instead of having each category having it's own form can I re-use the frmAddItem to serve all these categories?

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Re-using a form

    yes, ofcourse...you can do that...have a tag in some control's property, set the tag to required category and then based on the tag generate the query and display the frmAddItem...
    currently how are you doing it...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    This is the code in the command button for opening a new window:-
    VB Code:
    1. Private Sub cmdDirector_Click()
    2. Dim frmSel As New frmAddItem ' frmAddItem is the form that I want to re-use
    3.  
    4. frmSel.Show vbModal
    5.  
    6. Unload frmSel
    7. Set frmSel = Nothing
    8.  
    9. End Sub

    In the frmAddItem form_load event runs the following code:-
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim rst As ADODB.recordset
    4.     Dim db As ADODB.Connection
    5.    
    6.     Set db = New ADODB.Connection
    7.    
    8.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")      
    9.    
    10.     Set rst = New ADODB.recordset
    11.     With rst
    12.     .CursorLocation = adUseClient
    13.     .LockType = adLockBatchOptimistic
    14.     .Source = " SELECT producer FROM producer"
    15.     Set .ActiveConnection = db
    16.     .Open
    17.        End With
    18.            
    19.     ' populate listview control
    20.     Call PopulateList(ListView1, rst)  
    21.        
    22. End Sub

    PopulateList is the function for populating the ListView and it's code is:
    VB Code:
    1. Private Sub PopulateList(pList As ListView, _
    2.                          pRst As ADODB.recordset)
    3.     'On Error Resume Next
    4.     Dim i As Long
    5.     Dim iColCount As Long
    6.     Dim sColName As String
    7.     Dim sColValue As String
    8.     Dim oCH As ColumnHeader
    9.     Dim oLI As ListItem
    10.     Dim oSI As ListSubItem
    11.     Dim oFld As ADODB.Field
    12.  
    13.     With pList
    14.         .View = lvwReport
    15.         .GridLines = True
    16.         .FullRowSelect = True
    17.         .ListItems.Clear
    18.         .Sorted = False
    19.         pRst.MoveFirst
    20.        
    21.         ' set up column headers
    22.         For Each oFld In pRst.Fields
    23.             sColName = cn(oFld.Name)
    24.             Set oCH = .ColumnHeaders.Add()
    25.             oCH.Text = sColName
    26.             iColCount = iColCount + 1
    27.         Next oFld
    28.  
    29.         Do Until pRst.EOF
    30.             i = 0
    31.             ' setup fiprst column as a listitem
    32.             sColValue = cn(pRst.Fields(i).Value)
    33.             Set oLI = .ListItems.Add()
    34.             oLI.Text = sColValue
    35.            
    36.             pRst.MoveNext
    37.         Loop ' Next record
    38.         ' refresh it all
    39.         .Refresh
    40.         ' make sure 1st row can be seen
    41.         .ListItems(1).EnsureVisible
    42.     End With
    43. End Sub
    Last edited by osemollie; Aug 2nd, 2006 at 01:43 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    yes, ofcourse...you can do that...have a tag in some control's property, set the tag to required category and then based on the tag generate the query and display the frmAddItem...
    currently how are you doing it...
    ganeshmoorthy, mind telling me how I can set a tag in my control?

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    You can also use a property to do what you want, just add a property to frmAddItem, this property will modify a private variable value, this variable will determine the action to take (list actors, list directors, list producers) it will depend on the variable value.
    Last edited by jcis; Aug 2nd, 2006 at 03:51 AM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    If you dont mind just explain a little bit more on how I can do that. Am new to VB an just learning. How can I do what you have suggested? I will appreciate some sample code. Thanks.

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    Better use an Enum, In a VB module:
    VB Code:
    1. Public Enum enumListType
    2.     enuGenre = 0
    3.     enuActor = 1
    4.     enuProducer = 2
    5.     enuDirector = 3
    6. End Enum
    This is your new FrmMovies..
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdDirector_Click()
    4.     ShowList enuDirector
    5. End Sub
    6.  
    7. Private Sub cmdGenre_Click()
    8.     ShowList enuGenre
    9. End Sub
    10.  
    11. Private Sub cmdActor_Click()
    12.     ShowList enuActor
    13. End Sub
    14.  
    15. Private Sub cmdProducer_Click()
    16.     ShowList enuProducer
    17. End Sub
    18.  
    19. Private Sub ShowList(pType As enumListType)
    20. Dim frmSel As frmAdditem
    21.  
    22.     Set frmSel = New frmAdditem
    23.    
    24.     frmSel.ListType = pType
    25.     frmSel.LoadList
    26.     frmSel.Show vbModal
    27.     Set frmSel = Nothing
    28. End Sub
    And this is your new FrmAdditem
    VB Code:
    1. Option Explicit
    2.  
    3. Private mListType As enumListType
    4.  
    5. Public Property Let ListType(pnewVal As enumListType)
    6.     mListType = pnewVal
    7. End Property
    8.  
    9. Public Sub LoadList()
    10.     Dim rst As ADODB.Recordset
    11.     Dim db As ADODB.Connection
    12.    
    13.     Set db = New ADODB.Connection
    14.    
    15.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    16.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    17.    
    18.     Set rst = New ADODB.Recordset
    19.    
    20.     With rst
    21.         .CursorLocation = adUseClient
    22.         .LockType = adLockBatchOptimistic
    23.        
    24.         Select Case mListType
    25.             Case enuProducer
    26.                 .Source = " SELECT producer FROM producer"
    27.             Case enuDirector
    28.                 .Source = " SELECT director  FROM director "
    29.             Case enuActor
    30.                 .Source = " SELECT actor  FROM actor "
    31.             Case enuGenre
    32.                 .Source = " SELECT genre  FROM genre "
    33.         End Select
    34.        
    35.         Set .ActiveConnection = db
    36.         .Open
    37.     End With
    38.            
    39.     ' populate listview control
    40.     Call PopulateList(Me.ListView1, rst)
    41.    
    42.     rst.Close
    43.     db.Close
    44.     Set rst = Nothing
    45.     Set db = Nothing
    46. End Sub
    47.  
    48. Private Sub PopulateList(pList As ListView, pRst As ADODB.Recordset)
    49.     'On Error Resume Next
    50.     Dim i As Long
    51.     Dim iColCount As Long
    52.     Dim sColName As String
    53.     Dim sColValue As String
    54.     Dim oCH As ColumnHeader
    55.     Dim oLI As ListItem
    56.     Dim oSI As ListSubItem
    57.     Dim oFld As ADODB.Field
    58.  
    59.     With pList
    60.         .View = lvwReport
    61.         .GridLines = True
    62.         .FullRowSelect = True
    63.         .ListItems.Clear
    64.         .Sorted = False
    65.         pRst.MoveFirst
    66.        
    67.         ' set up column headers
    68.         For Each oFld In pRst.Fields
    69.             sColName = cn(oFld.Name)
    70.             Set oCH = .ColumnHeaders.Add()
    71.             oCH.Text = sColName
    72.             iColCount = iColCount + 1
    73.         Next oFld
    74.  
    75.         Do Until pRst.EOF
    76.             i = 0
    77.             ' setup fiprst column as a listitem
    78.             sColValue = cn(pRst.Fields(i).Value)
    79.             Set oLI = .ListItems.Add()
    80.             oLI.Text = sColValue
    81.            
    82.             pRst.MoveNext
    83.         Loop ' Next record
    84.         ' refresh it all
    85.         .Refresh
    86.         ' make sure 1st row can be seen
    87.         .ListItems(1).EnsureVisible
    88.     End With
    89. End Sub
    Thats all. Note, this is part in FrmAdditem:
    VB Code:
    1. Select Case mListType
    2.             Case enuProducer
    3.                 .Source = " SELECT producer FROM producer"
    4.             Case enuDirector
    5.                 .Source = " SELECT director  FROM director "
    6.             Case enuActor
    7.                 .Source = " SELECT actor  FROM actor "
    8.             Case enuGenre
    9.                 .Source = " SELECT genre  FROM genre "
    10.         End Select
    Replace those queries with the real queries you need.
    Last edited by jcis; Aug 2nd, 2006 at 04:40 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    THANKS jcis, it worked!! But having achieved that, my idea becomes a little more complicated since on this second form (frmAddItem), I have other buttons cmdAdd and cmdEdit which also open another common form (frmAddEdit).

    VB Code:
    1. Private Sub cmdEdit_Click()
    2.  With frmAddEdit
    3.         .Caption = .Caption & " Add Record"
    4.         .Show vbModal
    5.     End With
    6. End Sub

    And

    VB Code:
    1. Private Sub cmdEdit_Click()
    2. With frmAddEdit
    3.         .cmdAddSave.Caption = "&Save"
    4.         .cmdFinishCancel.Caption = "&Cancel"      
    5.             .txtAddEdit.Text = ListView1.SelectedItem.Text
    6.  .Show vbModal
    7.     End With
    8. End Sub

    And on the form frmAddEdit, I have the following code for adding?updating the items
    VB Code:
    1. Private Sub cmdAddSave_Click()
    2.     Select Case cmdAddSave.Caption
    3.         Case "&Add"
    4.             MsgBox "Code still under construction.", vbInformation, ""
    5.         Case "&Save"
    6.             '#### Update the database
    7.             strSql = "" & _
    8.                 "UPDATE producer " & _
    9.                 "SET " & _
    10.                     "producer = '" & txtAddEdit.Text & "', " & _                    
    11.                 "WHERE " & _
    12.                     "producerId = " & ttxtAddEdit..Text & " "
    13.             adoCn.Open
    14.             adoCn.Execute (strSql)
    15.             adoCn.Close
    16. cmdFinishCancel_Click
    17.     End Select
    18. End Sub

    Now, this being the case, how will I ensure that am updating the right table based on what I have listed in the ListView? Thanks in advance
    Last edited by osemollie; Aug 2nd, 2006 at 05:48 AM.

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    1. You need to add the same property to frmAddEdit, so frmAddEdit will know which table to use..
    VB Code:
    1. Private mListType As enumListType
    2.  
    3. Public Property Let ListType(pnewVal As enumListType)
    4.     mListType = pnewVal
    5. End Property

    2. Your ListView in FrmAddItem should have a column with IDs (it could be invisible if you want), something like..

    [Producer name] [ProducerId]
    ... ...
    (the same for producers, directors, genres and actors.)

    Then when the user selects a row and click EDIT, you pass the ID to frmAddEdit as a property, the same way you did before.

    3. frmAddEdit needs to know if it has to ADD or EDIT, you can pass that as a property also. Consider adding Remove/DELETE also.
    Last edited by jcis; Aug 2nd, 2006 at 06:21 AM.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    1. You need to add the same property to frmAddEdit, so frmAddEdit will know which table to use.
    Can I re-use the property I had used in the previous example?
    2. Your ListView in FrmAddItem should have a column with IDs, something like..

    [Producer name] [ProducerId]
    ... ...
    (the same for producers, directors, genres and actors.)

    Then when the user selects a row and click EDIT, you pass the ID to frmAddEdit as a property, the same way you did before.
    I have included the id columns in my query. But I don't know how call an ID as a property when a user selects and clicks Edit or clicks on ADD button.

    Am a bit confused when it comes to what I should put in the cmdAdd and cmdEdit buttons in the first form (frmAddItem) . Whats should it be? ShowList ???

    And what about in the cmdOK or cmdEdit buttons in the second form (frmAddEdit)

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    1. Not quite, you copy the propertty code (the code in jcis's last post) to frmAddEdit, then in FrmAddItem (before frmAddEdit is shown) set the value, eg:
    VB Code:
    1. With frmAddEdit
    2.   .ListType = mListType
    3. ..
    By using a property, you treat it just like you would treat other properties (eg: a Caption).


    2. What jcis meant is that you should create another property (as above) in frmAddEdit, which stores the ID. As the ID does not contain a small range of values you should not use an Enum - just a normal data type that is appropriate (eg: Integer or Long).

    You only need to set the value of this property if you are editing, as a new record will get a new ID when it is added.


    For the code in cmdAddSave_Click, you need to read the value of the properties - which are stored in your variables (mListType), and work with them appropriately.

    This will probably mean making another Select Case block (a bit like the end of post #7) inside each of the "&Add" and "&Save" blocks, so that you are building the right SQL statement for each table.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    2. What jcis meant is that you should create another property (as above) in frmAddEdit, which stores the ID. As the ID does not contain a small range of values you should not use an Enum - just a normal data type that is appropriate (eg: Integer or Long).

    You only need to set the value of this property if you are editing, as a new record will get a new ID when it is added.


    For the code in cmdAddSave_Click, you need to read the value of the properties - which are stored in your variables (mListType), and work with them appropriately.

    This will probably mean making another Select Case block (a bit like the end of post #7) inside each of the "&Add" and "&Save" blocks, so that you are building the right SQL statement for each table.
    I have tried understanding this bit but I can't get it. Anyone try putting it in code format for m to understand. Thanks

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    The code in post #9 is a property - make another one in frmAddEdit for storing the ID.

    Things to change from that code:
    • The property name (ListType) needs to be something appropriate (eg: TheID)
    • The data type needs to be Long instead of enumListType.
    • The variable name (mListType) needs to be something relevant (eg: mTheID).



    Once you have added the property, you can use it just like I used ListType in "1" above.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    From my first form (frmAddItem) I have:
    VB Code:
    1. Private Sub cmdAdd_Click()
    2. With frmAddItem
    3.         .Caption = .Caption & " Add New Item"
    4.         .TheID = mTheID
    5.         .Show vbModal
    6.     End With
    7.  
    8. End Sub
    and
    VB Code:
    1. Private Sub cmdEdit_Click()
    2. With frmAddItem
    3.         .Caption = .Caption & " Add New Item"
    4.         .TheID = mTheID
    5.         .cmdOK.Caption = "&Save"
    6.         .cmdCancel.Caption = "&Cancel"
    7.         .Show vbModal
    8.     End With
    9. End Sub

    In my second form (frmAddEdit) I have
    VB Code:
    1. Option Explicit
    2. Private mTheID As Long
    3.  
    4. Public Property Let TheID(pnewVal As Long)
    5.     mTheID = pnewVal
    6. End Property
    7.  
    8. Private Sub cmdCancel_Click()
    9. Unload Me
    10. End Sub
    11.  
    12. Private Sub cmdOk_Click()
    13. Dim rst As ADODB.Recordset
    14.     Dim db As ADODB.Connection
    15.    
    16.     Set db = New ADODB.Connection
    17.    
    18.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    19.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    20.    
    21.     Set rst = New ADODB.Recordset
    22.    
    23.     With rst
    24.         .CursorLocation = adUseClient
    25.         .LockType = adLockBatchOptimistic
    26.        
    27.     Select Case mTheID
    28.             Case enuProducer
    29.                 .Source = " INSERT INTO producer (producer) VALUES (" & "'" & txtItem.Text & "') "
    30.             Case enuDirector
    31.                 .Source = " INSERT INTO director (director) VALUES (" & "'" & txtItem.Text & "') "
    32.             Case enuWriter
    33.                 .Source = " INSERT INTO writer (writer) VALUES (" & "'" & txtItem.Text & "') "
    34.             Case enuGenre
    35.                 .Source = " INSERT INTO genre (genre) VALUES (" & "'" & txtItem.Text & "') "
    36. End Select
    37. End Sub
    38.  
    39.  
    40. Private Sub cmdUpdate_Click()
    41.  
    42. Dim rst As ADODB.Recordset
    43.     Dim db As ADODB.Connection
    44.    
    45.     Set db = New ADODB.Connection
    46.    
    47.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    48.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    49.    
    50.     Set rst = New ADODB.Recordset
    51.    
    52.     With rst
    53.         .CursorLocation = adUseClient
    54.         .LockType = adLockBatchOptimistic
    55. Select Case mListType
    56.             Case enuProducer
    57.                 .Source = " UPDATE producer SET [producer] = '" & Trim(txtItem.Text) & "' WHERE producerId = " & mListType & ""
    58.                 ase enuDirector
    59.                 .Source = " UPDATE director SET [director] = '" & Trim(txtItem.Text) & "' WHERE directorId = " & mListType & ""
    60.             Case enuWriter
    61.                 .Source = " UPDATE writer SET [writer] = '" & Trim(txtItem.Text) & "' WHERE writerId = " & mListType & ""
    62.             Case enuGenre
    63.                 .Source = " UPDATE genre SET [genre] = '" & Trim(txtItem.Text) & "' WHERE genreId = " & mListType & ""
    64. End Select
    65.            
    66. End Select
    67.  
    68. End Sub

    But right from the first form (frmAddItem) when i click on cmdAdd or Edit, i get an error "variable mTheID not set.

    Where am i going wrong?

    I also need to add cmdDelete button on form(frmAddItem)
    Last edited by osemollie; Aug 3rd, 2006 at 11:12 AM.

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    Your property TheID is good, but you seem to have lost the other one (ListType), and a few other bits too (eg: cmdUpdate_Click has two End Select's, and no End With).


    In frmAddItem, you are setting TheID in the wrong sub.
    It is pointless for an Add (as the ID doesn't exist yet), but is right for an Edit (as the value is known, and will let you work with the correct record).


    As to the value you use, you cannot simply use a variable (especially one which only exists in another form), you need to find the value from your form. Presumably it will be from the selected row in a list.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    As to the value you use, you cannot simply use a variable (especially one which only exists in another form), you need to find the value from your form. Presumably it will be from the selected row in a list.
    Am slightly lost here.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    2. Your ListView in FrmAddItem should have a column with IDs (it could be invisible if you want), something like..

    [Producer name] [ProducerId]
    ... ...
    (the same for producers, directors, genres and actors.)

    Then when the user selects a row and click EDIT, you pass the ID to frmAddEdit as a property, the same way you did before.

    3. frmAddEdit needs to know if it has to ADD or EDIT, you can pass that as a property also. Consider adding Remove/DELETE also.
    I now have this in my code
    VB Code:
    1. Option Explicit
    2. Private mListType As enumListType
    3. Private mTheID As Long
    4.  
    5. Public Property Let TheID(pnewVal As Long)
    6.     mTheID = pnewVal
    7. End Property
    8.  
    9. Public Property Let ListType(pnewVal As enumListType)
    10.     mListType = pnewVal
    11. End Property
    12.  
    13. Private Sub cmdCancel_Click()
    14. Unload Me
    15. End Sub
    16.  
    17. Private Sub cmdOk_Click()
    18. Dim rst As ADODB.Recordset
    19.     Dim db As ADODB.Connection
    20.    
    21.     Set db = New ADODB.Connection
    22.    
    23.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    24.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    25.    
    26.     Set rst = New ADODB.Recordset
    27.    
    28.     With rst
    29.         .CursorLocation = adUseClient
    30.         .LockType = adLockBatchOptimistic
    31.        
    32.     Select Case mListType
    33.             Case enuProducer
    34.                 .Source = " INSERT INTO producer (producer) VALUES (" & "'" & txtItem.Text & "') "
    35.             Case enuDirector
    36.                 .Source = " INSERT INTO director (director) VALUES (" & "'" & txtItem.Text & "') "
    37.             Case enuWriter
    38.                 .Source = " INSERT INTO writer (writer) VALUES (" & "'" & txtItem.Text & "') "
    39.             Case enuGenre
    40.                 .Source = " INSERT INTO genre (genre) VALUES (" & "'" & txtItem.Text & "') "
    41. End Select
    42. End With
    43. End Sub
    44.  
    45. Private Sub cmdUpdate_Click()
    46.  
    47. Dim rst As ADODB.Recordset
    48.     Dim db As ADODB.Connection
    49.    
    50.     Set db = New ADODB.Connection
    51.    
    52.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    53.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    54.    
    55.     Set rst = New ADODB.Recordset
    56.    
    57.     With rst
    58.         .CursorLocation = adUseClient
    59.         .LockType = adLockBatchOptimistic
    60. Select Case mListType
    61.             Case enuProducer
    62.                 .Source = " UPDATE producer SET [producer] = '" & Trim(txtItem.Text) & "' WHERE producerId = " & mListType & ""
    63.                 ase enuDirector
    64.                 .Source = " UPDATE director SET [director] = '" & Trim(txtItem.Text) & "' WHERE directorId = " & mListType & ""
    65.             Case enuWriter
    66.                 .Source = " UPDATE writer SET [writer] = '" & Trim(txtItem.Text) & "' WHERE writerId = " & mListType & ""
    67.             Case enuGenre
    68.                 .Source = " UPDATE genre SET [genre] = '" & Trim(txtItem.Text) & "' WHERE genreId = " & mListType & ""
    69.     End Select
    70. End With
    71. End Sub

    What else should I do/change to get it right?

  18. #18
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    Quote Originally Posted by osemollie
    Am slightly lost here.
    FrmAddItem has Listview, in that Listview you load names, you should also load IDs, you need those IDs to make Updates. I assume each table (director, genre, writer, producer) has an Autonumeric field ID, you should load this into your Listview.

    After you did that, your events would be..
    VB Code:
    1. Private Sub cmdAdd_Click()
    2.     With frmAddItem
    3.         .Caption = .Caption & " Add New Item"
    4.         .ListType = mListType
    5.         .Show vbModal
    6.     End With
    7. End Sub
    8.  
    9. Private Sub cmdEdit_Click()
    10. With frmAddItem
    11.         .Caption = .Caption & " Add New Item"
    12.         .ListType = mListType
    13.         .TheID = [B][Here you add the ID in the selected row in the Listview][/B]
    14.         .cmdOK.Caption = "&Save"
    15.         .cmdCancel.Caption = "&Cancel"
    16.         .Show vbModal
    17.     End With
    18. End Sub
    Then in FrmAddEdit..
    VB Code:
    1. Private mTheID      As Long
    2. Private mListType   As enumListType
    3.  
    4. Public Property Let ListType(pnewVal As enumListType)
    5.     mListType = pnewVal
    6. End Property
    7.  
    8. Public Property Let TheID(pnewVal As Long)
    9.     mTheID = pnewVal
    10. End Property
    11.  
    12. Private Sub cmdCancel_Click()
    13.     Unload Me
    14. End Sub
    15.  
    16. Private Sub cmdOk_Click()
    17. Dim db      As ADODB.Connection
    18. Dim StrSql  As String
    19.    
    20.     Set db = New ADODB.Connection
    21.    
    22.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    23.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    24.    
    25.        
    26.     Select Case mListType
    27.             Case enuProducer
    28.                 StrSql = " INSERT INTO producer (producer) VALUES ('" & txtItem.Text & "') "
    29.             Case enuDirector
    30.                 StrSql = " INSERT INTO director (director) VALUES ('" & txtItem.Text & "') "
    31.             Case enuWriter
    32.                 StrSql = " INSERT INTO writer (writer) VALUES ('" & txtItem.Text & "') "
    33.             Case enuGenre
    34.                 StrSql = " INSERT INTO genre (genre) VALUES ('" & txtItem.Text & "') "
    35.     End Select
    36.     db.Execute StrSql
    37.    
    38.     db.Close
    39.     Set db = Nothing
    40. End Sub
    41.  
    42. Private Sub cmdUpdate_Click()
    43. Dim StrSql  As String
    44. Dim db       As ADODB.Connection
    45.    
    46.     Set db = New ADODB.Connection
    47.    
    48.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    49.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    50.        
    51.     Select Case mListType
    52.           Case enuProducer
    53.                StrSql  = " UPDATE producer SET [producer] = '" & Trim(txtItem.Text) & "' WHERE producerId = " & mTheID & ""
    54.           Case enuDirector
    55.                StrSql  = " UPDATE director SET [director] = '" & Trim(txtItem.Text) & "' WHERE directorId = " & mTheID & ""
    56.           Case enuWriter
    57.               StrSql  = " UPDATE writer SET [writer] = '" & Trim(txtItem.Text) & "' WHERE writerId = " & mTheID & ""
    58.           Case enuGenre
    59.               StrSql = " UPDATE genre SET [genre] = '" & Trim(txtItem.Text) & "' WHERE genreId = " & mTheID & ""
    60.     End Select
    61.  
    62.     db.Execute StrSql
    63.  
    64.     db.Close
    65.     Set db = Nothing
    66. End Sub
    Last edited by jcis; Aug 3rd, 2006 at 12:15 PM.

  19. #19
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    I edited the code in my last post to remove TheID from cmdAdd_Click event (when you ADD you dont have to pass TheID, I assume this is an autonumeric field)

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    Am using
    VB Code:
    1. Private Sub cmdEdit_Click()
    2. With frmAddItem
    3.         .Caption = .Caption & " Add New Item"
    4.         .ListType = mListType
    5.         .TheID = ListView1.SelectedItem.Index
    6.         .cmdOK.Caption = "&Save"
    7.         .cmdCancel.Caption = "&Cancel"
    8.         .Show vbModal
    9.     End With
    10. End Sub
    in the first form but when it opens the new/second form, the value of the selected item is not displayed in the textbox. Is this the best way of determing the ID of the listed item in the Listview control?

  21. #21
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Re-using a form

    Example, you edit a producer name, then the FrmAddEdit needs 2 things:
    - A Producer ID: You are sending ListView1.SelectedItem.Index, are you sure its ok?, you should pass the ID of the producer not the index of the row.
    - A Producer Name: You are not sending it, send it to the textbox, its in the Listview also, something like (inside the With block):
    VB Code:
    1. .txtItem.Text = [get the producer name from the row selected in the ListView]
    By the way, do you know how to extract data from different cells in a Listview???, im not very familiar with this control, maybe somebody else could help you with this.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    For some strange reason, am unable to display info in two columns using this code:-
    VB Code:
    1. Public Sub LoadList()
    2.     Dim rst As ADODB.Recordset
    3.     Dim db As ADODB.Connection
    4.    
    5.     Set db = New ADODB.Connection
    6.    
    7.     db.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    8.         App.Path & "./databases/Visualtek.mdb;Persist Security Info=False")
    9.    
    10.     Set rst = New ADODB.Recordset
    11.    
    12.     With rst
    13.         .CursorLocation = adUseClient
    14.         .LockType = adLockBatchOptimistic
    15.        
    16.         Select Case mListType
    17.             Case enuProducer
    18.                 .Source = " SELECT producerId, producer FROM producer"
    19.             Case enuDirector
    20.                 .Source = " SELECT directorId, director  FROM director "
    21.             Case enuActor
    22.                 .Source = " SELECT actorId, actor  FROM actor "
    23.             Case enuGenre
    24.                 .Source = " SELECT genreId, genre  FROM genre "
    25.             Case enuWriter
    26.                 .Source = " SELECT writer, writerId FROM writer "
    27.            
    28.         End Select
    29.        
    30.         Set .ActiveConnection = db
    31.         .Open
    32.     End With
    33.            
    34.     ' populate listview control
    35.     Call PopulateList(ListView1, rst)
    36.    
    37.     rst.Close
    38.     db.Close
    39.     Set rst = Nothing
    40.     Set db = Nothing
    41. End Sub

    For populating the LsitView, I use
    VB Code:
    1. Private Sub PopulateList(pList As ListView, pRst As ADODB.Recordset)
    2.     'On Error Resume Next
    3.     Dim i As Long
    4.     Dim iColCount As Long
    5.     Dim sColName As String
    6.     Dim sColValue As String
    7.     Dim oCH As ColumnHeader
    8.     Dim oLI As ListItem
    9.     Dim oSI As ListSubItem
    10.     Dim oFld As ADODB.Field
    11.  
    12.     With pList
    13.         .View = lvwReport
    14.         .GridLines = True
    15.         .FullRowSelect = True
    16.         .ListItems.Clear
    17.         .Sorted = False
    18.         pRst.MoveFirst
    19.        
    20.         Do Until pRst.EOF
    21.             i = 0
    22.             ' setup fiprst column as a listitem
    23.             sColValue = cn(pRst.Fields(i).Value)
    24.             Set oLI = .ListItems.Add()
    25.             oLI.Text = sColValue
    26.            
    27.             pRst.MoveNext
    28.         Loop ' Next record
    29.         ' refresh it all
    30.         .Refresh
    31.         ' make sure 1st row can be seen
    32.         .ListItems(1).EnsureVisible
    33.     End With
    34. End Sub
    What could be wrong?

  23. #23
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    Your PopulateList sub is designed to only add one column. To add more you need to use code like this:
    VB Code:
    1. oLI.SubItems(1) = "your text"

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    si_the_geek, sorry, maybe you could elaborate more, I have been using this code to add even more than 5 columns on its own when I call the function in the form_load event. It is only that this time it is not in a form_load event but in another function (Public Sub LoadList()) that it is behaving funny. I have tried
    VB Code:
    1. sColValue = cn(pRst.Fields(1).Value)
    2.             Set oLI = .ListItems.Add()
    3.             oLI.Text = sColValue
    4.             oLI.SubItems(1) = cn(pRst.Fields(2).Value)
    but I get an error "Item can not be found in collection .." How do I set it properly?

  25. #25
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    The Fields collection starts at 0, so the first column is 0, and the second column is 1.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    Si, how can I do without you? ... Am almost there!!

    Now, after opening a new form and adding a new entry, how can I ensure that the ListView control in the first form is refreshed to reflect the new entry as soon as I close the second form?

  27. #27
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Re-using a form

    As you are showing the form Modally (using .Show vbModal) no more code in that sub will run until the form is closed - so simply re-fill the Listview after that line.

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    475

    Re: Re-using a form

    Guys, you were wonderful in this thread. Thanks for your patience and understanding. Special thanks to jcis and si_the_geek. God bless!!

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