Results 1 to 12 of 12

Thread: Populating listview on form_load [RESOLVED]

  1. #1

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Resolved Populating listview on form_load [RESOLVED]

    hello! I have searched the forum and found this Thread I tried it on my work but its giving me this "Argument not Optional" error and form_load event is highlighted.

    What i would like to achieved is that on form load, the listview(which has 6 columns) will display the records in my access database.

    When the records are already displayed, the user can single click any row and the same records displayed in that row will also display in 6 textboxes for editing purposes.

    Here's my code:
    VB Code:
    1. Option Explicit
    2. Dim rsStocks As ADODB.Recordset
    3. Dim sSQL$
    4. Private Sub cmdAdd_Click()
    5.   If CheckNullValue = False Then Exit Sub
    6.  
    7.   cmdCancel.Enabled = True
    8.   'lstStocks.Enabled = False
    9.   txtCode.SetFocus
    10.  
    11. On Error GoTo errHandler
    12.  
    13.   With oConn
    14.     .BeginTrans
    15.    
    16.     ' - save to tblStocks -
    17.     '--------------------------------------------------------------
    18.     .Execute "INSERT INTO tblStocks(Code, ProductDescription, UnitPrice, " & _
    19.                   "Quantity, ReOrder)" & _
    20.              "VALUES('" & txtCode.Text & "', '" & txtDesc.Text & "', '" & _
    21.                 txtUnitPrice.Text & "' ,'" & txtQty.Text & "','" & _
    22.                 txtROP.Text & "')", , adCmdText
    23.     '==============================================================
    24.     .CommitTrans
    25.   End With
    26.  
    27.   Call ClearFunction(frmStocks, "TextBox")
    28. Exit Sub
    29. errHandler:
    30.   oConn.RollbackTrans
    31.   Call msgError(Err)
    32. End Sub
    33.  
    34.  
    35. Private Sub cmdCancel_Click()
    36.   Call ClearFunction(frmStocks, "TextBox")
    37.  
    38. End Sub
    39.  
    40. Private Sub cmdClose_Click()
    41.   Unload Me
    42. End Sub
    43.  
    44. Private Sub Form_Load()
    45.   Call openConnection
    46.   Me.Left = LeftPos
    47.   Me.Top = TopPos
    48.  
    49.   Set rsStocks = New ADODB.Recordset
    50.   rsStocks.CursorLocation = adUseClient
    51.  
    52.  
    53.   sSQL = "SELECT * FROM tblStocks"
    54.  
    55.   If rsStocks.State = adStateOpen Then rsStocks.Close
    56.   rsStocks.Open sSQL, oConn, adOpenStatic, adLockOptimistic
    57.   Call FillListView
    58. End Sub
    59.  
    60. Private Sub Form_Unload(Cancel As Integer)
    61.   If rsStocks.State = adStateOpen Then rsStocks.Close
    62.   Set rsStocks = Nothing
    63. End Sub
    64.  
    65. 'TODO : return true if all required field has been filled
    66. Private Function CheckNullValue() As Boolean
    67.   CheckNullValue = False
    68.  
    69.   If Len(Trim$(txtCode.Text)) = 0 Then
    70.     Call msgMustBeFilled("Product Code")
    71.     txtCode.SetFocus
    72.     Exit Function
    73.   ElseIf Len(Trim$(txtDesc.Text)) = 0 Then
    74.     Call msgMustBeFilled("Description")
    75.     txtDesc.SetFocus
    76.     Exit Function
    77.   ElseIf Len(Trim$(txtUnitPrice.Text)) = 0 Then
    78.     Call msgMustBeFilled("Unit Price")
    79.     txtUnitPrice.SetFocus
    80.     Exit Function
    81.   ElseIf Len(Trim$(txtQty.Text)) = 0 Then
    82.     Call msgMustBeFilled("Quantity")
    83.     txtQty.SetFocus
    84.     Exit Function
    85.   ElseIf Len(Trim$(txtROP.Text)) = 0 Then
    86.     Call msgMustBeFilled("Re-Order Point")
    87.     txtROP.SetFocus
    88.     Exit Function
    89.   End If
    90.  
    91.   'If cmdAddCategory.Enabled = True Then Call ReplaceQuotation(txtCategoryName)
    92.   CheckNullValue = True
    93. End Function
    94.  
    95. Sub FillListView(lstStocks As ListView, rsStocks As ADODB.Recordset, Optional ImgNum As Long = 0)
    96.     lstStocks.ListItems.Clear
    97.     If Not rsStocks.BOF Then
    98.         rsStocks.MoveFirst
    99.         Dim a As Long
    100.         Dim lst As ListItem
    101.         While Not rsStocks.EOF
    102.             lst.Ghosted = True
    103.             Set lst = lstStocks.ListItems.Add(, , rsStocks.Fields(0).Value, , ImgNum)
    104.             For a = 1 To lv.ColumnHeaders.Count - 1
    105.                 lst.SubItems(a) = rsStocks.Fields(a).Value
    106.             Next
    107.             rsStocks.MoveNext
    108.         Wend
    109.     End If
    110. End Sub
    Last edited by Hack; Apr 23rd, 2006 at 06:39 AM.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Populating listview on form_load

    You have declared FillListView as,
    VB Code:
    1. Sub FillListView(lstStocks As ListView, rsStocks As ADODB.Recordset, Optional ImgNum As Long = 0)
    The third parameter, ImgNum, is optional. You may omit it.
    But the first 2 parameters are NOT optional. You must pass them to the procedure.
    But you aren't pasing anything to it in your call in Form_Load

    You should call it like,
    VB Code:
    1. Call FillListView lstStocks, rsStocks 'the third parameter is Optional

    Or,
    as both the listview and the recordset are in same scope, you can change the definition like,
    VB Code:
    1. Sub FillListView(Optional ImgNum As Long = 0)
    and then, your current call will work.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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

    Re: Populating listview on form_load

    The error "Argument not optional" means that you are trying to use a sub/function that has parameters, but have not given enough (or any in this case).

    and form_load event is highlighted.
    Next time, please also tell us which line is highlighted - as that is blatantly useful for us to know.

  4. #4

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    Thanks to you bought iPrank and si_the_geek.

    I'll try what u said iPrank.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  5. #5

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    ok. I have done these:
    on form_load
    VB Code:
    1. Call FillListView(lstStocks, rsStocks)

    on sub FillListView
    VB Code:
    1. Sub FillListView(lstStocks As ListView, rsStocks As ADODB.Recordset)
    2.     lstStocks.ListItems.Clear
    3.     If Not rsStocks.BOF Then
    4.         rsStocks.MoveFirst
    5.         Dim a As Long
    6.         Dim lst As ListItem
    7.         While Not rsStocks.EOF
    8.             lst.Ghosted = True
    9.             Set lst = lstStocks.ListItems.Add(, , rsStocks.Fields(0).Value)
    10.             For a = 1 To lstStocks.ColumnHeaders.Count - 1
    11.                 lst.SubItems(a) = rsStocks.Fields(a).Value
    12.             Next
    13.             rsStocks.MoveNext
    14.         Wend
    15.     End If
    16. End Sub

    I received this error "Object variable or With block variable not set"

    lst.Ghosted=true is highlighted.
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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

    Re: Populating listview on form_load

    Shouldn't that line be after the "Set lst = " line?

  7. #7

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    Thanks si_the_geek. PERFECT!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  8. #8

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    Now, on to my next question.

    When the records are already displayed, the user can single click or double click any row and the same records displayed in that row will also display in 6 textboxes for editing purposes.
    Is this possible? any sample please
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  9. #9

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    Anyone can help me please? I would like that whenever the user double click or perhaps single click on any of the records displayed in the listview, the same record will be displayed in textbox (in 6 texboxes because there are 6 columns in my listview).

    An example is much appreciated. Thanks!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  10. #10

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    I already figured it out! Thanks!
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Populating listview on form_load

    Quote Originally Posted by Simply Me
    I already figured it out! Thanks!
    Post your solution as it may benefit others.

  12. #12

    Thread Starter
    PowerPoster Simply Me's Avatar
    Join Date
    Aug 2003
    Posts
    2,748

    Re: Populating listview on form_load

    Quote Originally Posted by Hack
    Post your solution as it may benefit others.
    ok here's my code when the user click on any of the row (record) in my listview and display the same (record) in textboxes.
    VB Code:
    1. Private Sub LSTSTOCKS_ItemClick(ByVal Item As MSComctlLib.ListItem)
    2.     txtCode.Text = Item.Text
    3.     txtDesc.Text = Item.SubItems(1)
    4.     txtUnitPrice.Text = Format(Item.SubItems(2), "###,###,##0.00")
    5.     txtQty.Text = Item.SubItems(3)
    6.     txtROP.Text = Item.SubItems(4)
    7. End Sub
    To give is always to be NOBLE...
    To received is always to be BLESSED....
    Each day strive to be NOBLE
    Each day strive to be BLESSED

    If this post has helped you. Please take time to rate it.

    >=|+|=< Simply Me >=|+|=<

    ----------------------------------------
    Connection Strings | Number Only in Textbox | Splash Screen with Progress Bar | Printing to 1/2 of perforated bond paper |
    Freeze 2005 DataGridView Column

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