Solved

Listview columns

Posted on 2010-11-30
7
413 Views
Last Modified: 2012-05-10
I have copied and pasted some code to create a listview in VB of my data (sorry I cant actually credit it as I cant remember where I got it) I wanted to add more columns to the example project so added .ColumnHeaders.Add lines for the extra 9 lines needed however they dont seem to appear. I looked through the listview properties but couldnt find an obvious entry for number of columns.  Where am I going wrong with this?
Option Explicit

Private mobjConn                As ADODB.Connection
Private mobjCmd                 As ADODB.Command
Private mobjRst                 As ADODB.Recordset

Private mstrMaintMode           As String
Private mblnFormActivated       As Boolean
Private mblnUpdateInProgress    As Boolean

' Customer LV SubItem Indexes ...
Private Const mlngQ_No_IDX              As Long = 1
Private Const mlngQ_Number_IDX          As Long = 2
Private Const mlngQ_Initiator_IDX       As Long = 3
Private Const mlngQ_Description_IDX     As Long = 4
Private Const mlngQ_PB_IDX              As Long = 5
Private Const mlngQ_Axis_IDX            As Long = 6
Private Const mlngQ_Dead_IDX            As Long = 7
Private Const mlngQ_Position_IDX        As Long = 8
Private Const mlngQ_Delay_IDX           As Long = 9
Private Const mlngQ_Accel_IDX           As Long = 10
Private Const mlngQ_Decel_IDX           As Long = 11
Private Const mlngQ_Speed_IDX           As Long = 12
Private Const mlngQ_CopyCalc_IDX        As Long = 13
Private Const mlngQ_StartPos_IDX        As Long = 14
Private Const mlngQ_TravelDistance_IDX  As Long = 15
Private Const mlngQ_Time_IDX            As Long = 16
Private Const mlngQ_TimeincDelay_IDX    As Long = 17


'*****************************************************************************
'*                          General Form Events                              *
'*****************************************************************************

'-----------------------------------------------------------------------------
Private Sub Form_Load()
'-----------------------------------------------------------------------------
    
    CenterForm Me
    
    ConnectToDB
    
    SetupCustLVCols
    
    LoadCustomerListView
    
End Sub

'-----------------------------------------------------------------------------
Private Sub Form_Activate()
'-----------------------------------------------------------------------------
    
    If mblnFormActivated Then Exit Sub
    
    Refresh
    
    SetFormState True
    
    mblnFormActivated = True

End Sub


'*****************************************************************************
'*                        Command Button Events                              *
'*****************************************************************************


'-----------------------------------------------------------------------------
Private Sub cmdClose_Click()
'-----------------------------------------------------------------------------
    
    Unload Me

End Sub


'-----------------------------------------------------------------------------
Private Sub cmdSave_Click()
'-----------------------------------------------------------------------------

DoCmd.OpenReport "ReportName", acViewPreview

End Sub


'-----------------------------------------------------------------------------
Private Sub cmdCancel_Click()
'-----------------------------------------------------------------------------
    
    mblnUpdateInProgress = False
    SetFormState True
    lvwCustomer_ItemClick lvwCustomer.SelectedItem
    
End Sub



'*****************************************************************************
'*                      Other Control Events                                 *
'*****************************************************************************

Private Sub txtFirst_GotFocus()
    SelectTextboxText txtFirst
End Sub
Private Sub txtLast_GotFocus()
    SelectTextboxText txtLast
End Sub
Private Sub txtAddr_GotFocus()
    SelectTextboxText txtAddr
End Sub
Private Sub txtCity_GotFocus()
    SelectTextboxText txtCity
End Sub
Private Sub txtState_GotFocus()
    SelectTextboxText txtState
End Sub
Private Sub txtState_KeyPress(KeyAscii As Integer)
    If KeyAscii < 32 Then Exit Sub
    If Chr$(KeyAscii) >= "a" And Chr$(KeyAscii) <= "z" Then
        KeyAscii = KeyAscii - 32
    ElseIf Chr$(KeyAscii) >= "A" And Chr$(KeyAscii) <= "Z" Then
        ' OK
    Else
        KeyAscii = 0
    End If
End Sub
Private Sub txtState_Change()
    TabToNextTextBox txtState, txtZip
End Sub

Private Sub txtZip_GotFocus()
    SelectTextboxText txtZip
End Sub
Private Sub txtZip_KeyPress(KeyAscii As Integer)
    KeyAscii = DigitOnly(KeyAscii)
End Sub
Private Sub txtZip_Change()
    TabToNextTextBox txtZip, txtArea
End Sub

Private Sub txtArea_GotFocus()
    SelectTextboxText txtArea
End Sub
Private Sub txtArea_KeyPress(KeyAscii As Integer)
    KeyAscii = DigitOnly(KeyAscii)
End Sub
Private Sub txtArea_Change()
    TabToNextTextBox txtArea, txtPrfx
End Sub

Private Sub txtPrfx_GotFocus()
    SelectTextboxText txtPrfx
End Sub
Private Sub txtPrfx_KeyPress(KeyAscii As Integer)
    KeyAscii = DigitOnly(KeyAscii)
End Sub
Private Sub txtPrfx_Change()
    TabToNextTextBox txtPrfx, txtLine
End Sub

Private Sub txtLine_GotFocus()
    SelectTextboxText txtLine
End Sub
Private Sub txtLine_KeyPress(KeyAscii As Integer)
    KeyAscii = DigitOnly(KeyAscii)
End Sub


'*****************************************************************************
'*               Programmer-Defined Subs & Functions                         *
'*****************************************************************************

'-----------------------------------------------------------------------------
Private Sub ConnectToDB()
'-----------------------------------------------------------------------------

    Set mobjConn = New ADODB.Connection
    mobjConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                              & "Data Source=" _
                              & GetAppPath _
                              & "Cuesheet Created 20101124 1448.mdb"
    mobjConn.Open

    Set mobjCmd = New ADODB.Command
    Set mobjCmd.ActiveConnection = mobjConn
    mobjCmd.CommandType = adCmdText

End Sub

'-----------------------------------------------------------------------------
Private Sub DisconnectFromDB()
'-----------------------------------------------------------------------------

    Set mobjCmd = Nothing
    
    mobjConn.Close
    Set mobjConn = Nothing

End Sub

'-----------------------------------------------------------------------------
Private Sub SetFormState(pblnEnabled As Boolean)
'-----------------------------------------------------------------------------

    lvwCustomer.Enabled = pblnEnabled
    cmdClose.Enabled = pblnEnabled
    
    
    cmdSave.Enabled = Not pblnEnabled

End Sub

'-----------------------------------------------------------------------------
Private Sub SetupCustLVCols()
'-----------------------------------------------------------------------------
                                 
    With lvwCustomer
        .ColumnHeaders.Clear
        .ColumnHeaders.Add , , "QNUr", .Width * 0.15
        .ColumnHeaders.Add , , "Q", .Width * 0.09
        .ColumnHeaders.Add , , "Initiator", .Width * 0.05
        .ColumnHeaders.Add , , "Description", .Width * 0.3
        .ColumnHeaders.Add , , "P/B", .Width * 0.02
        .ColumnHeaders.Add , , "Axis", .Width * 0.15
        .ColumnHeaders.Add , , "Dead", .Width * 0.15
        .ColumnHeaders.Add , , "Position", .Width * 0.04
        .ColumnHeaders.Add , , "Delay", 0.04
        .ColumnHeaders.Add , , "Accel", 0.04
        .ColumnHeaders.Add , , "Decel", 0.04
        .ColumnHeaders.Add , , "Speed", 0.04
        .ColumnHeaders.Add , , "Copy Calc", 0.04
        .ColumnHeaders.Add , , "Start Pos", 0.04
        .ColumnHeaders.Add , , "Travel Distance", 0.04
        .ColumnHeaders.Add , , "Time", 0.04
        .ColumnHeaders.Add , , "Time inc delay", 0.04

        
    End With

End Sub

'-----------------------------------------------------------------------------
Private Sub LoadCustomerListView()
'-----------------------------------------------------------------------------
                                 
    Dim strSQL      As String
    Dim objCurrLI   As ListItem
    Dim strZip      As String
    Dim strPhone    As String
    Dim cueno As String
    Dim lngindex As Integer
                                 
    strSQL = "SELECT * FROM Act1 ORDER BY Q"
    
    mobjCmd.CommandText = strSQL
    Set mobjRst = mobjCmd.Execute
    
    lvwCustomer.ListItems.Clear
    
    With mobjRst
                cueno = ""
        Do Until .EOF
            Set objCurrLI = lvwCustomer.ListItems.Add(, , !Qno)
            If cueno = !Qno Then
            objCurrLI.SubItems(mlngQ_No_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = "" & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = "" & ""

            Else
            objCurrLI.SubItems(mlngQ_No_IDX) = !Q & ""
            objCurrLI.SubItems(mlngQ_Number_IDX) = !Initiator & ""
            objCurrLI.SubItems(mlngQ_Initiator_IDX) = !Description & ""
            End If
            objCurrLI.SubItems(mlngQ_Description_IDX) = !PB & ""
            objCurrLI.SubItems(mlngQ_PB_IDX) = !Axis & ""
            objCurrLI.SubItems(mlngQ_Axis_IDX) = !Dead & ""
            objCurrLI.SubItems(mlngQ_Dead_IDX) = !Pos & ""
            objCurrLI.SubItems(mlngQ_Position_IDX) = !Delay & ""
            objCurrLI.SubItems(mlngQ_Delay_IDX) = !Accel & ""
            objCurrLI.SubItems(mlngQ_Accel_IDX) = !Decel & ""
            objCurrLI.SubItems(mlngQ_Decel_IDX) = !Speed & ""
            objCurrLI.SubItems(mlngQ_Speed_IDX) = !CopyCalcTime & ""
            objCurrLI.SubItems(mlngQ_CopyCalc_IDX) = !StartPos & ""
            objCurrLI.SubItems(mlngQ_StartPos_IDX) = !TravelDist & ""
            objCurrLI.SubItems(mlngQ_TravelDistance_IDX) = !MoveTime & ""
            objCurrLI.SubItems(mlngQ_Time_IDX) = !Moveincdelay & ""
   cueno = !Qno
            .MoveNext
        Loop
    End With
   
End Sub

Open in new window

0
Comment
Question by:simonwait
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 34239951
If you are just wanting to know how many columns...

ListView.ColumnHeaders.Count
0
 
LVL 1

Author Comment

by:simonwait
ID: 34240230
Well that returns 17 which is correct - however they listview actually only showing 8 columns.  What I am trying to figure out is if there is something stopping the rest of the columns from displaying.  Below is a screenshot of the resulting view.  It should show all the columns but doesnt

Capture.PNG
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 400 total points
ID: 34240782
try to do because 0.04 is too small to see

.ColumnHeaders.Add , , "Delay", 0.04
        .ColumnHeaders.Add , , "Accel", .Width * 0.04
        .ColumnHeaders.Add , , "Decel", .Width * 0.04
        .ColumnHeaders.Add , , "Speed", .Width * 0.04
        .ColumnHeaders.Add , , "Copy Calc", .Width * 0.04
        .ColumnHeaders.Add , , "Start Pos", .Width * 0.04
        .ColumnHeaders.Add , , "Travel Distance", .Width * 0.04
        .ColumnHeaders.Add , , "Time", .Width * 0.04
        .ColumnHeaders.Add , , "Time inc delay", .Width * 0.04

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 400 total points
ID: 34240788
also

.ColumnHeaders.Add , , "Delay", .Width * 0.04
0
 
LVL 14

Assisted Solution

by:Brook Braswell
Brook Braswell earned 100 total points
ID: 34241190
It looks to me like you are trying to set these columns at a percentage of your width but you left the ".width" out of the remaining headers

so the first 8 columns you have are about 95% of the width of your list view....


 With lvwCustomer
        .ColumnHeaders.Clear
        .ColumnHeaders.Add , , "QNUr", .Width * 0.15
        .ColumnHeaders.Add , , "Q", .Width * 0.09
        .ColumnHeaders.Add , , "Initiator", .Width * 0.05
        .ColumnHeaders.Add , , "Description", .Width * 0.3
        .ColumnHeaders.Add , , "P/B", .Width * 0.02
        .ColumnHeaders.Add , , "Axis", .Width * 0.15
        .ColumnHeaders.Add , , "Dead", .Width * 0.15
        .ColumnHeaders.Add , , "Position", .Width * 0.04

 ' from here down you left out .width so they have no size...

' hence I add the .Width to give them a better width
        .ColumnHeaders.Add , , "Delay", .Width * 0.04
        .ColumnHeaders.Add , , "Accel", .Width * 0.04
        .ColumnHeaders.Add , , "Decel", .Width * 0.04
        .ColumnHeaders.Add , , "Speed", .Width * 0.04
        .ColumnHeaders.Add , , "Copy Calc", .Width * 0.04
        .ColumnHeaders.Add , , "Start Pos", .Width * 0.04
        .ColumnHeaders.Add , , "Travel Distance", .Width * 0.04
        .ColumnHeaders.Add , , "Time", .Width * 0.04
        .ColumnHeaders.Add , , "Time inc delay",.Width *  0.04

       
    End With

0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 34241458
I do not see the horizontal scroll on your screen shot so I am wondering if you have done something to prevent this from showing - similar to incorrectly setting the column widths
0
 
LVL 1

Author Closing Comment

by:simonwait
ID: 34247365
To be honest EDDKYT got there first with the solution however I thought Brook1966's answer explain the reasons better so I though I would give some points to him too.  Gave the second bit of EDDKYT's answer points simply to tie it in with the solution.  Hope this is fair
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Vb.net Listview 9 76
How does CurrentUser work? 10 25
VBA Shell can't Find Word document 11 43
Add and format columns in vb6 7 22
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now