Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Listview columns

Posted on 2010-11-30
7
Medium Priority
?
437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 26

Assisted Solution

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

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

Assisted Solution

by:Brook Braswell
Brook Braswell earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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