Link to home
Start Free TrialLog in
Avatar of simonwait
simonwaitFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Listview columns

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

Avatar of Brook Braswell
Brook Braswell
Flag of United States of America image

If you are just wanting to know how many columns...

ListView.ColumnHeaders.Count
Avatar of simonwait

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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