Solved

MS Access VBA ListViewCtrl Object Positioning Problems

Posted on 2006-07-06
11
1,486 Views
Last Modified: 2012-05-05
Hi Everyone,

I have a MS Access VBA project and I've included Microsoft ListViewCtrl object on the form. When I fill the ListViewCtrl, the ListViewCtrl for some reason is displayed in the top left corner rather than where it's positioned. There's a white outline where the list control should be but the data and the column headers are at the top of the form.

Here's the code to fill up the list:

Private Sub FillRegistryList()
   
 
    If Me.TreatyNo.Column(0) <> "" Then
        Dim strSQL As String
        Dim rsFind As DAO.Recordset
        Dim intCount As Integer
        Dim lstitmAdd As ListItem
         
     strSQL = "Select * from tblRegistry where [TreatyNo] = " & Me.TreatyNo.Column(0)
     
         Dim db As DAO.Database
         Set db = CurrentDb()
       
         Set rsFind = db.OpenRecordset(strSQL, dbOpenSnapshot)
                           
                   DoCmd.Hourglass True
                           
                   DoEvents
                                           
                       Do Until rsFind.EOF
                             intCount = intCount + 1
                         Set lstitmAdd = Me![lstRegistry].ListItems.Add(, "'RegID = " & CStr(rsFind("RegID")) & "'", CStr(rsFind("RegID")))
                         lstitmAdd.ListSubItems.Add , , rsFind("MapID")
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("TreatyNo"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Code"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Qtr"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Sec"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Twnshp"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Range"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Acres"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Land-type"))
                     
Nextrecord:
                           rsFind.MoveNext
                        Loop
                                   
                    rsFind.Close
                    Set rsFind = Nothing
                    DoCmd.Hourglass False
                    lblRegistryItems.Caption = "Registry Items(s): " & intCount & " "
   

    Me.Repaint
     
    End If
   
End Sub

Any advice is appreciated. Thanks for any assistance in advance!
0
Comment
Question by:mance
11 Comments
 
LVL 6

Expert Comment

by:AHMKC1
ID: 17049091
It shows top- left corner becuase you don't set its top and left properties

when you use this  'Dim lstitmAdd As ListItem' code it take default properties top=0 and left=0


set lstitmAdd.top and lstitmAdd.Left properties as you required

Thanks
0
 

Author Comment

by:mance
ID: 17050706
Thanks AHMKC1,

For some reason I'm unable to set the ListItem ... it almost seems like it's read-only.

I get the position for the ListViewCtrl and through each loop I set the ListItem control to the Left position of the ListViewCtrl and the Top position of the ListViewCtrl plus the row width. However, the ListItem doesn't seem to want to accept the new coordinates. Do I have to set the position elsewhere?

Thanks for your help.

Here's my code.

If Me.TreatyNo.Column(0) <> "" Then
        Dim strSQL As String
        Dim rsFind As DAO.Recordset
        Dim intCount As Integer
        Dim lstitmAdd As ListItem
       
       
        Dim PosX, PosY As Single
        PosX = lstRegistry.Left
        PosY = lstRegistry.Top
             
         
     strSQL = "Select * from tblRegistry where [TreatyNo] = " & Me.TreatyNo.Column(0)
     
         Dim db As DAO.Database
         Set db = CurrentDb()
       
         Set rsFind = db.OpenRecordset(strSQL, dbOpenSnapshot)
                           
                   DoCmd.Hourglass True
                           
                   DoEvents
                                           
                       Do Until rsFind.EOF
                             intCount = intCount + 1
                         Set lstitmAdd = Me![lstRegistry].ListItems.Add(, "'RegID = " & CStr(rsFind("RegID")) & "'", CStr(rsFind("RegID")))
                        lstitmAdd.Top = PosY + 210
                        lstitmAdd.Left = PosX
                         lstitmAdd.ListSubItems.Add , , rsFind("MapID")
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("TreatyNo"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Code"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Qtr"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Sec"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Twnshp"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Range"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Acres"))
                         lstitmAdd.ListSubItems.Add , , CheckRSFields(rsFind("Land-type"))
                       
                   
                       Debug.Print "lstitmAdd.Top=" & lstitmAdd.Top & " lstitmAdd.Left=" & lstitmAdd.Left
Nextrecord:
                           rsFind.MoveNext
                        Loop
                                   
                    rsFind.Close
                    Set rsFind = Nothing
                   

                    DoCmd.Hourglass False
                    lblRegistryItems.Caption = "Registry Items(s): " & intCount & " "
   

   Debug.Print "lstRegistry.Top=" & lstRegistry.Top & " lstRegistry.Left=" & lstRegistry.Left
   
    Set lstitmAdd = Nothing
     Me.Repaint
     
    End If
   
End Sub
0
 
LVL 84
ID: 17051755
I may be reading this wrong, but isn't the ListItem object is an actual item in the list ... it's not the actual Listview you're using ...

You can set the Top and Left properties of the ListView item directly:

Me.ListView2.Top = xx
Me.ListView2.Left = xx

I believe these are twips, so 1 in = 1440 (roughly)

0
 

Author Comment

by:mance
ID: 17053292
Hi LSMConsulting,

ListItem is an item contained in lstRegistry. lstRegistry is a Microsoft ListViewCtrl. The problem is that the ListItems aren't being displayed in the proper position - they're not contained in lstRegistry but instead are displayed on the Top,Left position of the form.
0
 
LVL 84
ID: 17053509
I've got to ask - why are you setting the .Top and .Left properties of the ListItems? Typically you allow the ListView to handle the display characteristics of the listitems.

However, in your code, what are the values of PosX and PosY? Put a breakpoint in your code and type this in the Immediate window:

?Posx

?Posy

after the line where they are set ... betting you'll find some odd values there.

BTW, you have PosX being typed as a Variant. If you want both typed as Single:

Dim Posx As Single, PosY As Single

                       
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mance
ID: 17054868
Hi LSMConsulting,

Thanks for the advice.

Read AHMKC1's comment on why I'm trying to set the position of the ListItems.

When I do a debug.print ( Debug.Print "lstitmAdd.Top=" & lstitmAdd.Top & " lstitmAdd.Left=" & lstitmAdd.Left) the values of the the ListItems aren't what I assigned to them and are positions set at the top, left part of the form. I don't know why, because the ListViewCtrl values are where they should be - that is the ListViewCtrl is a white outline and the data and headers are positioned off in the top left corner.

Hopefully this is somewhat clear. Am I forgetting to set some sort of property???

Thanks!
0
 
LVL 84
ID: 17054987
I guess my point is - why assign positions to the ListItems? I've seen a LOT of ListView code, and I can't recall seeing anyone set the .Top and .Left property of each individual ListItem ...

Have you tried commenting out these two lines:

lstitmAdd.Top = PosY + 210
lstitmAdd.Left = PosX

to see if your listitems show correctly in the Listview control?

If not, perhaps you could Zip your send your database to the www.ee-stuff.com website? If you login (using your EE username and password), you can click the Experts tab and upload your file there. Or, you can send it to me, and I'll be glad to do it for you.

0
 

Author Comment

by:mance
ID: 17055938
Thanks LSMConsulting for the generous offer.

I have a very crude solution ... the ListViewCtrl is contained in a TabControl and I noticed when you tab from the current page to a different page and then back the ListViewCtrl will display itself correct with all rows and columns contained in the list.

I don't know why this is, but if someone knows of why this is happening, I'd really like to know.

This is the silly code I used after I filled the list up with data:

    Me.PageTest.Visible = True
    Me.Cultivated.SetFocus
    Me.PageTest.SetFocus
    Me.Cultivated.SetFocus
    Me.PageTest.Visible = False

Thanks for everyone's efforts.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
ID: 17057275
Ahh ... on a tab control ... that's a different story. There are known issues with certain versions of the Listview and Access, the most common being the one you're describing. One common solution is to make the LIstview invisible, then only make it visible when your tab is in focus. Don't know why but this usually works

me.YourListView.visible = (me.YourTabControl= YourPageIndex)

You can do the above on the Tab's Change event.

Also, some use the Timer control to do this, but I tend to avoid this:

(note: set the Timer Interval = 1, or something like that)
Private Sub Form_Timer()
On Error Resume Next
Me.TimerInterval = 0
DoCmd.MoveSize , , Me.WindowWidth + 10, Me.WindowHeight
DoCmd.MoveSize , , Me.WindowWidth - 10, Me.WindowHeight
End Sub
0
 
LVL 27

Expert Comment

by:jjafferr
ID: 17461490
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: LSMConsulting {http:#17057275}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

jjafferr
EE Cleanup Volunteer
0

Featured Post

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

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

747 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

13 Experts available now in Live!

Get 1:1 Help Now