|
-
Apr 22nd, 2006, 10:48 AM
#1
Thread Starter
PowerPoster
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:
Option Explicit
Dim rsStocks As ADODB.Recordset
Dim sSQL$
Private Sub cmdAdd_Click()
If CheckNullValue = False Then Exit Sub
cmdCancel.Enabled = True
'lstStocks.Enabled = False
txtCode.SetFocus
On Error GoTo errHandler
With oConn
.BeginTrans
' - save to tblStocks -
'--------------------------------------------------------------
.Execute "INSERT INTO tblStocks(Code, ProductDescription, UnitPrice, " & _
"Quantity, ReOrder)" & _
"VALUES('" & txtCode.Text & "', '" & txtDesc.Text & "', '" & _
txtUnitPrice.Text & "' ,'" & txtQty.Text & "','" & _
txtROP.Text & "')", , adCmdText
'==============================================================
.CommitTrans
End With
Call ClearFunction(frmStocks, "TextBox")
Exit Sub
errHandler:
oConn.RollbackTrans
Call msgError(Err)
End Sub
Private Sub cmdCancel_Click()
Call ClearFunction(frmStocks, "TextBox")
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub Form_Load()
Call openConnection
Me.Left = LeftPos
Me.Top = TopPos
Set rsStocks = New ADODB.Recordset
rsStocks.CursorLocation = adUseClient
sSQL = "SELECT * FROM tblStocks"
If rsStocks.State = adStateOpen Then rsStocks.Close
rsStocks.Open sSQL, oConn, adOpenStatic, adLockOptimistic
Call FillListView
End Sub
Private Sub Form_Unload(Cancel As Integer)
If rsStocks.State = adStateOpen Then rsStocks.Close
Set rsStocks = Nothing
End Sub
'TODO : return true if all required field has been filled
Private Function CheckNullValue() As Boolean
CheckNullValue = False
If Len(Trim$(txtCode.Text)) = 0 Then
Call msgMustBeFilled("Product Code")
txtCode.SetFocus
Exit Function
ElseIf Len(Trim$(txtDesc.Text)) = 0 Then
Call msgMustBeFilled("Description")
txtDesc.SetFocus
Exit Function
ElseIf Len(Trim$(txtUnitPrice.Text)) = 0 Then
Call msgMustBeFilled("Unit Price")
txtUnitPrice.SetFocus
Exit Function
ElseIf Len(Trim$(txtQty.Text)) = 0 Then
Call msgMustBeFilled("Quantity")
txtQty.SetFocus
Exit Function
ElseIf Len(Trim$(txtROP.Text)) = 0 Then
Call msgMustBeFilled("Re-Order Point")
txtROP.SetFocus
Exit Function
End If
'If cmdAddCategory.Enabled = True Then Call ReplaceQuotation(txtCategoryName)
CheckNullValue = True
End Function
Sub FillListView(lstStocks As ListView, rsStocks As ADODB.Recordset, Optional ImgNum As Long = 0)
lstStocks.ListItems.Clear
If Not rsStocks.BOF Then
rsStocks.MoveFirst
Dim a As Long
Dim lst As ListItem
While Not rsStocks.EOF
lst.Ghosted = True
Set lst = lstStocks.ListItems.Add(, , rsStocks.Fields(0).Value, , ImgNum)
For a = 1 To lv.ColumnHeaders.Count - 1
lst.SubItems(a) = rsStocks.Fields(a).Value
Next
rsStocks.MoveNext
Wend
End If
End Sub
Last edited by Hack; Apr 23rd, 2006 at 06:39 AM.
-
Apr 22nd, 2006, 11:41 AM
#2
Re: Populating listview on form_load
You have declared FillListView as,
VB Code:
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:
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:
Sub FillListView(Optional ImgNum As Long = 0)
and then, your current call will work.
-
Apr 22nd, 2006, 11:42 AM
#3
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.
-
Apr 22nd, 2006, 07:07 PM
#4
Thread Starter
PowerPoster
Re: Populating listview on form_load
Thanks to you bought iPrank and si_the_geek.
I'll try what u said iPrank.
-
Apr 22nd, 2006, 07:16 PM
#5
Thread Starter
PowerPoster
Re: Populating listview on form_load
ok. I have done these:
on form_load
VB Code:
Call FillListView(lstStocks, rsStocks)
on sub FillListView
VB Code:
Sub FillListView(lstStocks As ListView, rsStocks As ADODB.Recordset)
lstStocks.ListItems.Clear
If Not rsStocks.BOF Then
rsStocks.MoveFirst
Dim a As Long
Dim lst As ListItem
While Not rsStocks.EOF
lst.Ghosted = True
Set lst = lstStocks.ListItems.Add(, , rsStocks.Fields(0).Value)
For a = 1 To lstStocks.ColumnHeaders.Count - 1
lst.SubItems(a) = rsStocks.Fields(a).Value
Next
rsStocks.MoveNext
Wend
End If
End Sub
I received this error "Object variable or With block variable not set"
lst.Ghosted=true is highlighted.
-
Apr 22nd, 2006, 07:20 PM
#6
Re: Populating listview on form_load
Shouldn't that line be after the "Set lst = " line?
-
Apr 22nd, 2006, 07:27 PM
#7
Thread Starter
PowerPoster
Re: Populating listview on form_load
Thanks si_the_geek. PERFECT!
-
Apr 22nd, 2006, 07:29 PM
#8
Thread Starter
PowerPoster
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
-
Apr 23rd, 2006, 06:13 AM
#9
Thread Starter
PowerPoster
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!
-
Apr 23rd, 2006, 06:21 AM
#10
Thread Starter
PowerPoster
Re: Populating listview on form_load
I already figured it out! Thanks!
-
Apr 23rd, 2006, 06:39 AM
#11
Re: Populating listview on form_load
 Originally Posted by Simply Me
I already figured it out! Thanks!
Post your solution as it may benefit others.
-
Apr 23rd, 2006, 11:17 PM
#12
Thread Starter
PowerPoster
Re: Populating listview on form_load
 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:
Private Sub LSTSTOCKS_ItemClick(ByVal Item As MSComctlLib.ListItem)
txtCode.Text = Item.Text
txtDesc.Text = Item.SubItems(1)
txtUnitPrice.Text = Format(Item.SubItems(2), "###,###,##0.00")
txtQty.Text = Item.SubItems(3)
txtROP.Text = Item.SubItems(4)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|