Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2007, 09:57 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Okay, bear with me here since I don't know the language, but try this:

Set ctlSource = Me.lstVisitors

   For intCurrentRow = 0 To ctlSource.ListCount - 1
        If ctlSource.Selected(intCurrentRow) Then

        iFK = ctlSource.Column(0, intCurrentRow)
        sStaff = ctlSource.Column(1, intCurrentRow)
        sReden = ctlSource.Column(2, intCurrentRow)
        sAanlog = ctlSource.Column(3, intCurrentRow)
        rst.AddNew
        rst("NaamVisitor") = iFK
        rst("VoorStaff") = sStaff
        rst("VoorReden") = sReden
        rst("InvoerDoor") = sAanlog
        rst("InvoerOp") = Now()
        rst.Update
        End If
    Next intCurrentRow

Anyway, if the names don't match up with the columns, you will have to make changes, but you see what I am trying to do here. This should take all the values from the list box and paste them one for one into the table.

Did that help?

mmcdonal
 
Old May 4th, 2007, 10:38 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't need the

        sStaff = ctlSource.Column(1, intCurrentRow)
        sReden = ctlSource.Column(2, intCurrentRow)
        sAanlog = ctlSource.Column(3, intCurrentRow)

cause they are asigned another way.

I have tried someting and this tells me that it does not use the adding to the table. I changed the code to

MsgBox "Step 1"
sAanlog = "Admin"

'insert every visitor in the table and print the badge
Set db = Application.CurrentDb()
Set rst = db.OpenRecordset("tblOpvolgingBezoekers", dbOpenDynaset)

Set ctlSource = Me.lstVisitors

   For intCurrentRow = 0 To ctlSource.ListCount - 1
        If ctlSource.Selected(intCurrentRow) Then

        iFK = ctlSource.Column(0, intCurrentRow)
        rst.AddNew
        rst("NaamVisitor") = iFK
        rst("VoorStaff") = sStaff
        rst("VoorReden") = sReden
        rst("InvoerDoor") = sAanlog
        rst("InvoerOp") = Now()
        rst.Update
        MsgBox "Step 1b"
        End If
        MsgBox "Step 2"
    Next intCurrentRow

rst.Close
Set rst = Nothing
Set db = Nothing

MsgBox "Step 3"

When I run this, I first get the msgbox "Step 1" to apear, then the number of times I have elements in the list box "Step 2" appears and at the end "Step 3". However the msgbox "Step 1b" in the middle does NEVER appear, so it seems like it is not using the code... to insert the data, but just is only looping through if you see what I mean here...

Any idea why this happens?

 
Old May 4th, 2007, 11:15 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Modify the code like this and see what happens:

For intCurrentRow = 0 To ctlSource.ListCount - 1
        'If ctlSource.Selected(intCurrentRow) Then

        iFK = ctlSource.Column(0, intCurrentRow)
        rst.AddNew
        rst("NaamVisitor") = iFK
        rst("VoorStaff") = sStaff
        rst("VoorReden") = sReden
        rst("InvoerDoor") = sAanlog
        rst("InvoerOp") = Now()
        rst.Update
        MsgBox "Step 1b"
        'End If
        MsgBox "Step 2"
    Next intCurrentRow

This drops the If Then statement and it should run no matter what.

Did that help?


mmcdonal
 
Old May 4th, 2007, 11:25 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

After looking at this last solution, it occurs to me that:

1. The user isn't actually selecting anything in the list box. It is just being used for a visual accumulation of the selected data. Therefore:

If ctlSource.Selected(intCurrentRow) Then

   will never be true.

2. You want to loop through ALL of the items in the list box anyway, so there is no need to condition the addnew/updates anyway.

*smacking forehead*


mmcdonal
 
Old May 4th, 2007, 11:26 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

In fact, you could even say:

If ctlSource.NotSelected(intCurrentRow) Then

but again, no need to make this conditional.


mmcdonal
 
Old May 4th, 2007, 01:53 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok,

when I put a ' in front of the if line and the end if, I get the error

Item not found in this collection (error number 3265).

Any other idea, cause I'm trying to follow you here, but this is hard to understand completely for me... doing the best I can

 
Old May 4th, 2007, 02:13 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Well, I think the time has come to stop messing around. Here is your psuedo code:

On Click of the make badge button (correct?)

Instead of using the List Box, use the table on which the list box is built. So for tblTempListBox do this:

sSQL1 = "SELECT TableID FROM tblTempListBox"
sSQL2 = "SELECT * FROM tblTableWhereThisIsGoing"
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset

rs1.Open sSQL1, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rs2.Open sSQL2, CurrentProject.Connection, adOpenDynamic, adLockOptimistic

If rs1.RecordCount <> 0 Then
   Do Until rs1.EOF
        rs2.AddNew
        rs2("NaamVisitor") = rs1("TableID")
        rs2("VoorStaff") = sStaff
        rs2("VoorReden") = sReden
        rs2("InvoerDoor") = sAanlog
        rs2("InvoerOp") = Now()
        rs2.Update
   rs1.MoveNext
   Loop
End If

rs1.Close
rs2.Close

That should do it.

Then runa delete query on the temp table, then requery the list box to show it empty.

Did that work?






mmcdonal
 
Old May 6th, 2007, 01:29 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Made it able to work thanks to you...

Thanks very much






Similar Threads
Thread Thread Starter Forum Replies Last Post
add scrollbar to table MAntis_sg Classic ASP Basics 3 May 18th, 2006 11:36 AM
Error 3201: Cannot add record in child form/table HomeShow Access VBA 0 October 5th, 2004 09:56 AM
Data has changed error with linked ODBC table Wim Leys Access 7 June 8th, 2004 06:05 AM
add same data in diff table / search yylee Access VBA 8 April 13th, 2004 02:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.