home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / databa1a / frmmain.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1999-08-25  |  27.5 KB  |  708 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Listbox/Listview/Database Population Example"
  6.    ClientHeight    =   4884
  7.    ClientLeft      =   36
  8.    ClientTop       =   264
  9.    ClientWidth     =   7116
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   4884
  14.    ScaleWidth      =   7116
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.CommandButton cmd_lvRandomPop 
  17.       Caption         =   "random pop"
  18.       Height          =   372
  19.       Left            =   5160
  20.       TabIndex        =   14
  21.       Top             =   3480
  22.       Width           =   1272
  23.    End
  24.    Begin VB.CommandButton cmd_lvPopDBLock 
  25.       Caption         =   "lock n' pop db"
  26.       Height          =   372
  27.       Left            =   5160
  28.       TabIndex        =   13
  29.       Top             =   4440
  30.       Width           =   1272
  31.    End
  32.    Begin VB.CommandButton cmd_lvPopFileLock 
  33.       Caption         =   "lock n' pop file"
  34.       Height          =   372
  35.       Left            =   5160
  36.       TabIndex        =   12
  37.       Top             =   3960
  38.       Width           =   1272
  39.    End
  40.    Begin VB.CommandButton cmd_lvDumpDB 
  41.       Caption         =   "dump to db"
  42.       Height          =   372
  43.       Left            =   4080
  44.       TabIndex        =   11
  45.       Top             =   2520
  46.       Width           =   972
  47.    End
  48.    Begin VB.CommandButton cmd_lvClear 
  49.       Caption         =   "clear list"
  50.       Height          =   372
  51.       Left            =   4080
  52.       TabIndex        =   10
  53.       Top             =   3480
  54.       Width           =   972
  55.    End
  56.    Begin VB.CommandButton cmd_lvPopDB 
  57.       Caption         =   "pop db"
  58.       Height          =   372
  59.       Left            =   4080
  60.       TabIndex        =   9
  61.       Top             =   4440
  62.       Width           =   972
  63.    End
  64.    Begin VB.CommandButton cmd_lvDumpFile 
  65.       Caption         =   "dump to file"
  66.       Height          =   372
  67.       Left            =   4080
  68.       TabIndex        =   8
  69.       Top             =   3000
  70.       Width           =   972
  71.    End
  72.    Begin VB.CommandButton cmd_lvPopFile 
  73.       Caption         =   "pop file"
  74.       Height          =   372
  75.       Left            =   4080
  76.       TabIndex        =   7
  77.       Top             =   3960
  78.       Width           =   972
  79.    End
  80.    Begin MSComctlLib.ListView ListView1 
  81.       Height          =   4752
  82.       Left            =   60
  83.       TabIndex        =   6
  84.       Top             =   60
  85.       Width           =   3912
  86.       _ExtentX        =   6900
  87.       _ExtentY        =   8382
  88.       View            =   3
  89.       LabelWrap       =   -1  'True
  90.       HideSelection   =   -1  'True
  91.       FullRowSelect   =   -1  'True
  92.       _Version        =   393217
  93.       ForeColor       =   -2147483640
  94.       BackColor       =   -2147483643
  95.       BorderStyle     =   1
  96.       Appearance      =   1
  97.       NumItems        =   3
  98.       BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  99.          Text            =   "Data1"
  100.          Object.Width           =   2540
  101.       EndProperty
  102.       BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  103.          SubItemIndex    =   1
  104.          Text            =   "Data2"
  105.          Object.Width           =   2540
  106.       EndProperty
  107.       BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  108.          SubItemIndex    =   2
  109.          Text            =   "Data3"
  110.          Object.Width           =   2540
  111.       EndProperty
  112.    End
  113.    Begin VB.CommandButton cmd_lbPopFile 
  114.       Caption         =   "pop file"
  115.       Height          =   372
  116.       Left            =   6000
  117.       TabIndex        =   5
  118.       Top             =   1560
  119.       Width           =   972
  120.    End
  121.    Begin VB.CommandButton cmd_lbDumpFile 
  122.       Caption         =   "dump to file"
  123.       Height          =   372
  124.       Left            =   6000
  125.       TabIndex        =   4
  126.       Top             =   600
  127.       Width           =   972
  128.    End
  129.    Begin VB.CommandButton cmd_lbPopDB 
  130.       Caption         =   "pop db"
  131.       Height          =   372
  132.       Left            =   6000
  133.       TabIndex        =   3
  134.       Top             =   2040
  135.       Width           =   972
  136.    End
  137.    Begin VB.CommandButton cmd_lbListClear 
  138.       Caption         =   "clear list"
  139.       Height          =   372
  140.       Left            =   6000
  141.       TabIndex        =   2
  142.       Top             =   1080
  143.       Width           =   972
  144.    End
  145.    Begin VB.CommandButton cmd_lbDumpDB 
  146.       Caption         =   "dump to db"
  147.       Height          =   372
  148.       Left            =   6000
  149.       TabIndex        =   1
  150.       Top             =   120
  151.       Width           =   972
  152.    End
  153.    Begin VB.ListBox List1 
  154.       Height          =   2352
  155.       Left            =   4020
  156.       TabIndex        =   0
  157.       Top             =   60
  158.       Width           =   1872
  159.    End
  160. Attribute VB_Name = "frmMain"
  161. Attribute VB_GlobalNameSpace = False
  162. Attribute VB_Creatable = False
  163. Attribute VB_PredeclaredId = True
  164. Attribute VB_Exposed = False
  165. Option Explicit
  166. ' This program was written and tested on a system
  167. ' with the following specifications:
  168. ' CPU: Intel Pentium II 400Mhz
  169. ' RAM: 128 MB
  170. ' OS: Windows 98
  171. ' Visual Basic 6.0
  172. ' NOTE: "Microsoft DAO 3.51 Object Library" MUST be referenced.
  173. '                To do so, select Project > References from the menu. In the
  174. '                 resulting dialog box select "Microsoft DAO 3.51 Object Library"
  175. '                 and click OK.
  176. ' This stops an hWnd from being updated. It should speed up
  177. ' our listbox/listview population considerably. Pass it an hWnd
  178. ' to lock, to unlock, pass zero (0).
  179. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
  180. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  181. Private Const LVM_FIRST = &H1000
  182. Private Const LVM_DELETEALLITEMS = (LVM_FIRST + 9)
  183. ' Crate new data access object variables
  184. Private dbWorkspace As Workspace
  185. Private dbDatabase As Database
  186. Private dbTable As Recordset
  187. Private dbName As Field
  188. Private dbBday As Field
  189. Private sFileName As String
  190. Public Function TrimNull(ByVal What As String)
  191.     ' Function: TrimNull
  192.     '
  193.     ' Takes a string as input and returns
  194.     ' a string with  all instances of the
  195.     ' null character Chr$(0), leading and
  196.     ' trailing spaces, and vbCrLf removed.
  197.     What = Trim$(What)
  198.     If InStr(What, Chr(0)) <> 0 Then What = RemoveChar(What, Chr(0))
  199.     If InStr(What, vbCrLf) <> 0 Then What = RemoveChar(What, vbCrLf)
  200.         
  201.     Do: DoEvents
  202.     If Right(What, 1) = Chr(10) Or Right(What, 1) = Chr(13) Then What = Mid(What, 1, Len(What) - 1)
  203.     Loop Until Right(What, 1) <> Chr(10) And Right(What, 1) <> Chr(13)
  204.     Do: DoEvents
  205.     If Left(What, 1) = Chr(10) Or Left(What, 1) = Chr(13) Then What = Mid(What, 2)
  206.     Loop Until Left(What, 1) <> Chr(10) And Left(What, 1) <> Chr(13)
  207.     TrimNull = What
  208. End Function
  209. Public Function GetListItem(ByVal xItem As ListItem, Optional ByVal sDelimiter As String = "") As String
  210. ' Function: GetListItem
  211. ' Arguments: ListItem - Listview item you want returned as a string
  212. '                        sDelimiter - Delimiter you want inserted between each subitem
  213. ' Returns: A string containing all subitems seperated by sDelimiter
  214. Dim i As Integer
  215. ' First we set GetListItem to the main item's text, tag, and check status
  216. GetListItem = xItem.Text & sDelimiter & xItem.Tag & sDelimiter & xItem.Checked
  217. ' Then we loop through the subitems and add their text to each item
  218. For i = 1 To xItem.ListSubItems.Count
  219.     GetListItem = GetListItem & sDelimiter & xItem.SubItems(i)
  220. Next i
  221. End Function
  222. Public Function FileExists(Optional ByVal sFileName As Variant, Optional ByVal sPath As Variant) As Boolean
  223.     ' By PCC MikeD
  224.     ' Function: FileExists
  225.     '
  226.     ' Checks whether sFileName exists in sPath.
  227.     ' If only sPath is passed to the function,
  228.     ' the existance of sPath is checked.
  229.     ' Returns either 'True' or 'False'
  230.     On Error GoTo Oops
  231.     If IsMissing(sPath) Then
  232.         'Only a file name was passed.
  233.         If Len(Dir$(sFileName)) Then FileExists = True
  234.     Else
  235.         'A directory was passed
  236.         'Append a backslash to the pathname, if necessary.
  237.         If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
  238.         If IsMissing(sFileName) Then
  239.             'Directory was passed, but not a file, so determine if
  240.             'the directory exists
  241.             If Len(Dir$(sPath, vbDirectory)) Then FileExists = True
  242.         Else
  243.             'Both a directory and a file were passed, so determine
  244.             'if the file exists in the specified directory.
  245.             If Len(Dir$(sPath & sFileName)) Then FileExists = True
  246.         End If
  247.     End If
  248.     Exit Function
  249. Oops:
  250.     Exit Function
  251. End Function
  252. Public Function LoadList(ByVal sFileName As String, Optional lbList As ListBox, Optional lvList As ListView) As Boolean
  253. ' Make sure a list was passed to us
  254. If TypeName(lbList) = "Nothing" And TypeName(lvList) = "Nothing" Then LoadList = False: Exit Function
  255. Dim fileNum As Integer ' freefile
  256. Dim i As Long ' loop counter
  257. Dim j As Integer ' loop counter
  258. Dim sDelimiter As String
  259. Dim fileInput As String
  260. Dim fileArray As Variant
  261. Dim itemArray As Variant
  262. Dim xItem As ListItem
  263. sDelimiter = "$^e&p%q#!" ' We want something extremely unique to delimit
  264.                                                      ' each of the subitems if we get a listview. This
  265.                                                      ' should do it, but you can change this here
  266.                                                      ' if necessary
  267. ' Find a free file number to use
  268. fileNum = FreeFile
  269. If Not FileExists(sFileName) Then Exit Function
  270. Open sFileName For Input As fileNum
  271.     i = FileLen(sFileName)
  272.     ' The file passed doesn't contain any data. Exit the function
  273.     If i = 0 Then Close fileNum: LoadList = False: Exit Function
  274.     fileInput = Input(i, fileNum)
  275. Close fileNum
  276.     ' If the user passes us a listview, but the delimiter is not present in the file,
  277.     ' then the file was not saved using the sister function, SaveList. Exit.
  278.     If (Not TypeName(lvList) = "Nothing") And (InStr(fileInput, sDelimiter) = 0) Then LoadList = False: Exit Function
  279.     fileArray = Split(fileInput, vbCrLf)
  280.     If (Not TypeName(lbList) = "Nothing") Then ' A listbox was passed, loop through it
  281.         For i = 0 To UBound(fileArray)
  282.             lbList.AddItem fileArray(i)
  283.         Next i ' = 0 To UBound(fileArray)
  284.     Else ' A listview was passed. loop through it and it's subitems
  285.         For i = 0 To UBound(fileArray)
  286.             If (Not TrimNull(fileArray(i)) = "") Then
  287.                 itemArray = Split(fileArray(i), sDelimiter)
  288.                 For j = 0 To UBound(itemArray)
  289.                     Select Case j
  290.                         Case 0
  291.                             Set xItem = lvList.ListItems.Add(, , itemArray(j))
  292.                         Case 1
  293.                             xItem.Tag = itemArray(j)
  294.                         Case 2
  295.                             If itemArray(j) = "True" Then xItem.Checked = True
  296.                         Case Else
  297.                             xItem.SubItems(j - 2) = itemArray(j)
  298.                     End Select ' j
  299.                 Next j ' = 0 To UBound(itemArray)
  300.             End If ' (Not TrimNull(hdrArray(i)) = "")
  301.         Next i ' = 0 To UBound(fileArray)
  302.     End If ' (TypeName(lbList) = "Nothing")
  303. LoadList = True
  304.                 
  305. End Function
  306. Public Function SaveList(ByVal sFileName As String, Optional lbList As ListBox, Optional lvList As ListView, Optional ByVal sCriteria As String, Optional ByVal iSubItem As Integer, Optional ByVal useTag As Boolean = False, Optional matchCase As Boolean = True) As Boolean
  307. ' Make sure a list was passed to us
  308. If TypeName(lbList) = "Nothing" And TypeName(lvList) = "Nothing" Then SaveList = False: Exit Function
  309. Dim fileNum As Integer
  310. Dim i As Long
  311. Dim sDelimiter As String
  312. Dim curItem As String
  313. sDelimiter = "$^e&p%q#!" ' We want something extremely unique to delimit
  314.                                                      ' each of the subitems if we get a listview. This
  315.                                                      ' should do it, but you can change this here
  316.                                                      ' if necessary
  317. ' Find a free file number to use
  318. fileNum = FreeFile
  319. If FileExists(sFileName) Then Kill (sFileName)
  320. Open sFileName For Append As fileNum
  321.     If Not TypeName(lbList) = "Nothing" Then ' A listbox was passed, loop through it
  322.         For i = 0 To lbList.ListCount - 1
  323.             If (sCriteria <> "") Then
  324.                 If (matchCase = True) Then
  325.                     If InStr(lbList.List(i), sCriteria) Then Print #fileNum, lbList.List(i)
  326.                 Else
  327.                     If InStr(LCase(lbList.List(i)), LCase(sCriteria)) Then Print #fileNum, lbList.List(i)
  328.                 End If ' (matchCase = True)
  329.             Else
  330.                 Print #fileNum, lbList.List(i)
  331.             End If ' (sCriteria <> "")
  332.         Next i ' i = 0 To lbList.ListCount - 1
  333.     Else ' A listview was passed. loop through it and it's subitems
  334.         For i = 1 To lvList.ListItems.Count
  335.             If (sCriteria <> "") Then
  336.                 If (useTag = True) Then
  337.                     curItem = lvList.ListItems.item(i).Tag
  338.                     If (matchCase = True) Then
  339.                         If curItem Like sCriteria Then Print #fileNum, GetListItem(lvList.ListItems.item(i), sDelimiter)
  340.                     Else
  341.                         If LCase(curItem) Like LCase(sCriteria) Then Print #fileNum, GetListItem(lvList.ListItems.item(i), sDelimiter)
  342.                     End If ' (matchCase = True)
  343.                 Else
  344.                     ' If iSubItem is 0 then use the item.text instead
  345.                     If iSubItem <> 0 Then curItem = lvList.ListItems.item(i).SubItems(iSubItem) Else curItem = lvList.ListItems.item(i).Text
  346.                     If (matchCase = True) Then
  347.                         If curItem Like sCriteria Then Print #fileNum, GetListItem(lvList.ListItems.item(i), sDelimiter)
  348.                     Else
  349.                         If LCase(curItem) Like LCase(sCriteria) Then Print #fileNum, GetListItem(lvList.ListItems.item(i), sDelimiter)
  350.                     End If ' (matchCase = True)
  351.                 End If ' (useTag = True)
  352.             Else
  353.                 Print #fileNum, GetListItem(lvList.ListItems.item(i), sDelimiter)
  354.             End If ' (sCriteria <> "")
  355.         Next i ' i = 1 To lvList.ListItems.Count
  356.     End If
  357. Close fileNum
  358. SaveList = True
  359.                 
  360. End Function
  361. Private Sub cmd_lbDumpDB_Click()
  362. Dim tmr As New CStopWatch, i As Long
  363.     tmr.Reset
  364.     ' Create data access objects
  365.     Set dbWorkspace = DBEngine.Workspaces(0)
  366.     ' Change database path if necessary
  367.     Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  368.     ' Open the table we want to work on
  369.     Set dbTable = dbDatabase.OpenRecordset("List1", dbOpenTable)
  370.     For i = 0 To List1.ListCount - 1
  371.         ' Add new record
  372.         dbTable.AddNew
  373.         
  374.         ' Set the field to the current list item
  375.         dbTable!Elements = List1.List(i)
  376.         ' Tell the DB to update the current record (i.e. save the field)
  377.         dbTable.Update
  378.         
  379.         ' Jump to the last record, not sure if this is necessray
  380.         ' for dumping a list ot a DB.
  381.         ' dbTable.MoveLast
  382.     Next i
  383.     dbTable.Close
  384.     dbWorkspace.Close
  385.     Debug.Print "[" & Time & "] DB dump successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  386. End Sub
  387. Private Sub cmd_lbDumpFile_Click()
  388. Dim tmr As New CStopWatch, i
  389. tmr.Reset
  390. Open "g:\temp\newsreader\db dump.txt" For Append As #1
  391.     For i = 0 To List1.ListCount - 1
  392.         Print #1, List1.List(i)
  393.     Next i
  394. Close #1
  395. Debug.Print "[" & Time & "] File dump successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  396. End Sub
  397. Private Sub cmd_lbListClear_Click()
  398. List1.Clear
  399. End Sub
  400. Private Sub cmd_lbPopDB_Click()
  401.     Dim i As Integer
  402.     Dim tmr As New CStopWatch
  403.     tmr.Reset
  404.     ' Create data access objects
  405.     Set dbWorkspace = DBEngine.Workspaces(0)
  406.     ' Change database path if necessary
  407.     Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  408.     ' Open the table we want to work on
  409.     Set dbTable = dbDatabase.OpenRecordset("List1", dbOpenTable)
  410.     'Use special handling if new database (i.e. no records in the table)
  411.     If dbTable.BOF And dbTable.EOF Then
  412.         Debug.Print "[" & Time & "] No Records Found."
  413.         dbTable.Close
  414.         dbWorkspace.Close
  415.     End If
  416.     'Start on first record
  417.     dbTable.MoveFirst
  418.     Do Until dbTable.EOF
  419.         List1.AddItem dbTable!Elements
  420.         dbTable.MoveNext
  421.     Loop
  422.     ' We're finished so close the table and DB
  423.     ' and exit
  424.     dbTable.Close
  425.     dbWorkspace.Close
  426.     Debug.Print "[" & Time & "] DB population successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  427. End Sub
  428. Private Sub cmd_lbPopFile_Click()
  429. Dim tmr As New CStopWatch, i As Long
  430. Dim fileInput$, fileArray As Variant
  431. tmr.Reset
  432. Open "g:\temp\newsreader\db dump.txt" For Input As #1
  433.     i = FileLen("g:\temp\newsreader\db dump.txt")
  434.     fileInput = Input(i, 1)
  435. Close #1
  436.  fileArray = Split(fileInput, vbCrLf)
  437. For i = 0 To UBound(fileArray)
  438.     List1.AddItem fileArray(i)
  439. Next i
  440. Debug.Print "[" & Time & "] File population successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  441. End Sub
  442. Private Sub cmd_lvClear_Click()
  443. Dim tmr As New CStopWatch
  444. tmr.Reset
  445.  ' We don't want to use listview1.listitems.clear
  446.  ' because it seems to take a lot longer to clear
  447.  ' the list. To see these results for yourself,
  448.  ' uncomment the following line and comment out
  449.  ' the line w/ sendmessage.
  450.  ' NOTE: Doing this will cause VB to appear to hang.
  451. ' ListView1.ListItems.Clear
  452. SendMessage Me.ListView1.hwnd, LVM_DELETEALLITEMS, 0, 0
  453. Debug.Print "[" & Time & "] Listview successfully cleared in " & tmr.Elapsed / 1000 & " seconds."
  454. End Sub
  455. Private Sub cmd_lvDumpDB_Click()
  456. Dim i As Long, itmx As ListItem, tmr As New CStopWatch
  457.         
  458.     tmr.Reset
  459.     ' Create data access objects
  460.     Set dbWorkspace = DBEngine.Workspaces(0)
  461.     ' Change database path if necessary
  462.     Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  463.     ' Open the table we want to work on
  464.     Set dbTable = dbDatabase.OpenRecordset("List2", dbOpenTable)
  465.     ' Loop through the items in the listview and dump to DB
  466.     For i = 1 To ListView1.ListItems.Count
  467.         ' Set itmx to the current listview item
  468.         Set itmx = ListView1.ListItems.item(i)
  469.         ' Add new record
  470.         dbTable.AddNew
  471.         
  472.         ' Set each field in the new record to the
  473.         ' corresponding listview item
  474.         dbTable!Data1 = itmx.Text
  475.         dbTable!Data2 = itmx.SubItems(1)
  476.         dbTable!Data3 = itmx.SubItems(2)
  477.         dbTable!Tag = itmx.Tag
  478.         ' Tell the DB to update the current record (i.e. save the field)
  479.         dbTable.Update
  480.         
  481.         ' Jump to the last record, not sure if this is necessray
  482.         ' for dumping a list ot a DB.
  483.         ' dbTable.MoveLast
  484.     Next i
  485.     dbTable.Close
  486.     dbWorkspace.Close
  487.     Debug.Print "[" & Time & "] Listview DB dump successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  488. End Sub
  489. Private Sub cmd_lvDumpFile_Click()
  490. Dim i, tmr As New CStopWatch
  491. tmr.Reset
  492. SaveList "g:\temp\newsreader\listview dump.txt", , ListView1
  493. Debug.Print "[" & Time & "] Listview FILE dump successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  494. End Sub
  495. Private Sub cmd_lvPopDB_Click()
  496.     Dim i As Integer, itmx As ListItem
  497.     Dim tmr As New CStopWatch
  498.     tmr.Reset
  499.     ' Create data access objects
  500.     Set dbWorkspace = DBEngine.Workspaces(0)
  501.     ' Change database path if necessary
  502.     Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  503.     ' Open the table we want to work on
  504.     Set dbTable = dbDatabase.OpenRecordset("List2", dbOpenTable)
  505.     'Use special handling if new database (i.e. no records in the table)
  506.     If dbTable.BOF And dbTable.EOF Then
  507.         Debug.Print "[" & Time & "] No Records Found."
  508.         dbTable.Close
  509.         dbWorkspace.Close
  510.     End If
  511.     'Start on first record
  512.     dbTable.MoveFirst
  513.     ' As per dogbert's suggestion, we use "With"
  514.     ' instead of dbTable.xxx. This has no effect on
  515.     ' clearing the listview as expected. No effect on
  516.     ' LV population either
  517.     With dbTable
  518.             Do While (.EOF = False)
  519.                 Set itmx = ListView1.ListItems.Add(, , !Data1)
  520.                 itmx.SubItems(1) = !Data2
  521.                 itmx.SubItems(2) = !Data3
  522.                 itmx.Tag = !Tag
  523.                 
  524.                 .MoveNext
  525.             
  526.             Loop
  527.         ' We're finished so close the table and DB
  528.         ' and exit
  529.         .Close
  530.         dbWorkspace.Close
  531.         
  532.     End With
  533.     Debug.Print "[" & Time & "] Listview DB population successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  534. End Sub
  535. Private Sub cmd_lvPopDBLock_Click()
  536.     Dim i As Integer, itmx As ListItem
  537.     Dim tmr As New CStopWatch
  538.     ' lock listview1 - don't let it update
  539.     ' this will speed up operation
  540.     LockWindowUpdate ListView1.hwnd
  541.     tmr.Reset
  542.     ' Create data access objects
  543.     Set dbWorkspace = DBEngine.Workspaces(0)
  544.     ' Change database path if necessary
  545.     Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  546.     ' Open the table we want to work on
  547.     Set dbTable = dbDatabase.OpenRecordset("List2", dbOpenTable)
  548.     'Use special handling if new database (i.e. no records in the table)
  549.     If dbTable.BOF And dbTable.EOF Then
  550.         Debug.Print "[" & Time & "] No Records Found."
  551.         LockWindowUpdate 0
  552.         ListView1.Refresh
  553.         dbTable.Close
  554.         dbWorkspace.Close
  555.     End If
  556.     'Start on first record
  557.     dbTable.MoveFirst
  558.     Do Until dbTable.EOF
  559.         
  560.         Set itmx = ListView1.ListItems.Add(, , dbTable!Data1)
  561.         itmx.SubItems(1) = dbTable!Data2
  562.         itmx.SubItems(2) = dbTable!Data3
  563.         itmx.Tag = dbTable!Tag
  564.         
  565.         ' Move on to the next item in the DB
  566.         dbTable.MoveNext
  567.         
  568.     Loop
  569.     ' We're finished so close the table and DB
  570.     ' and exit
  571.     dbTable.Close
  572.     dbWorkspace.Close
  573.     ' unlock listview1 so we can see the items
  574.     LockWindowUpdate 0
  575.     ListView1.Refresh
  576.     Debug.Print "[" & Time & "] Listview DB population w/ LOCKING successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  577.     Exit Sub
  578. End Sub
  579. Private Sub cmd_lvPopFile_Click()
  580. Dim tmr As New CStopWatch, i
  581. tmr.Reset
  582. LoadList "g:\temp\newsreader\listview dump.txt", , ListView1
  583. Debug.Print "[" & Time & "] Listview FILE population successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  584. End Sub
  585. Private Sub cmd_lvPopFileLock_Click()
  586. Dim tmr As New CStopWatch
  587. ' lock listview1 - don't let it update
  588. ' this will speed up operation
  589. LockWindowUpdate ListView1.hwnd
  590. tmr.Reset
  591. LoadList "g:\temp\newsreader\listview dump.txt", , ListView1
  592. ' unlock listview1 so we can see the items
  593. LockWindowUpdate 0
  594. ListView1.Refresh
  595. Debug.Print "[" & Time & "] Listview FILE population w/ LOCKING successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  596. End Sub
  597. Private Sub cmd_lvRandomPop_Click()
  598. Dim StartTime, EndTime, i As Long, itmx As ListItem
  599.         StartTime = GetTickCount
  600.         
  601.         ' Populate the listview control
  602.         For i = 1 To 50000
  603.             Set itmx = ListView1.ListItems.Add(, , "abc" & i)
  604.             itmx.SubItems(1) = "def" & i
  605.             itmx.SubItems(2) = "ghi" & i
  606.             itmx.Tag = "jkl" & i
  607.         Next i
  608.         
  609.         EndTime = GetTickCount - StartTime
  610.         
  611.         Debug.Print "[" & Time & "] Random LISTVIEW generation successfully completed in " & EndTime / 1000 & " seconds."
  612.         
  613. End Sub
  614. Private Sub Form_Load()
  615. Dim i As Long, itmx As ListItem
  616. Dim popStart As Integer
  617. Dim tmr As New CStopWatch
  618. ' Path to the test database. Change here if necessary
  619.  sFileName = "abc123.mdb"
  620. ' This controls whether or not we are
  621. ' going to populate our controls on
  622. ' startup and which control to fill.
  623. ' 0 = none, 1 = listview, 2 = listbox
  624. ' 3 = pop from DB to LV
  625. popStart = 0
  626. Select Case popStart
  627.         
  628.     Case 1
  629.         tmr.Reset
  630.         
  631.         ' Populate the listview control
  632.         For i = 1 To 50000
  633.             Set itmx = ListView1.ListItems.Add(, , "abc" & i)
  634.             itmx.SubItems(1) = "def" & i
  635.             itmx.SubItems(2) = "ghi" & i
  636.             itmx.Tag = "jkl" & i
  637.         Next i
  638.         Debug.Print "[" & Time & "] Random LISTVIEW generation successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  639.         
  640.     Case 2
  641.         tmr.Reset
  642.         
  643.         ' Populate our listbox
  644.         For i = 1 To 10000
  645.             List1.AddItem "abc" & i
  646.         Next i
  647.         
  648.         Debug.Print "[" & Time & "] Random LISTBOX generation successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  649.         
  650.     Case 3
  651.         tmr.Reset
  652.         
  653.         ' Create data access objects
  654.         Set dbWorkspace = DBEngine.Workspaces(0)
  655.         
  656.         ' Change database path if necessary
  657.         Set dbDatabase = dbWorkspace.OpenDatabase(sFileName)
  658.         
  659.         ' Open the table we want to work on
  660.         Set dbTable = dbDatabase.OpenRecordset("List2", dbOpenTable)
  661.         
  662.         'Start on first record
  663.         dbTable.MoveFirst
  664.         Do Until dbTable.EOF
  665.             
  666.             'Set itmx = ListView1.ListItems.Add(, "abc" & Format(Time, "hhmmss") & ListView1.ListItems.Count + 1, dbTable!Data1)
  667.             Set itmx = ListView1.ListItems.Add(, , dbTable!Data1)
  668.             itmx.SubItems(1) = dbTable!Data2
  669.             itmx.SubItems(2) = dbTable!Data3
  670.             itmx.Tag = dbTable!Tag
  671.             
  672.             ' Move on to the next item in the DB
  673.             dbTable.MoveNext
  674.             
  675.         Loop
  676.         
  677.         ' We're finished so close the table and DB
  678.         ' and exit
  679.         dbTable.Close
  680.         dbWorkspace.Close
  681.         
  682.         Debug.Print "[" & Time & "] Listview DB population successfully completed in " & tmr.Elapsed / 1000 & " seconds."
  683. End Select
  684. End Sub
  685. Public Function RemoveChar$(ByVal Buf As String, ByVal Char As String)
  686.     ' Function: RemoveChar$
  687.     '
  688.     ' Removes all occurrences of 'Char' in 'Buf'
  689.     ' and returns the new string
  690.     Dim Check%
  691.     Dim LeftOf$, RightOf$
  692.     Do
  693.         DoEvents
  694.         Check% = InStr(Buf$, Char$)
  695.         If Check% > 0 Then
  696.             LeftOf$ = Left(Buf$, Check% - 1)
  697.             RightOf$ = Right(Buf$, Len(Buf$) - Check%)
  698.             Buf$ = LeftOf$ & RightOf$
  699.         End If
  700.     Loop Until Check% = 0
  701.     RemoveChar$ = Buf$
  702. End Function
  703. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  704. End Sub
  705. Private Sub ListView1_ItemClick(ByVal item As MSComctlLib.ListItem)
  706. Me.Caption = "Listbox/Listview/Database Population Example - " & item.Tag
  707. End Sub
  708.