Solved

help with DAO.  adding selected listbox items from two seperate listboxes

Posted on 2003-12-11
16
1,143 Views
Last Modified: 2007-12-19

'''Right here I have all items in "ListBoxWaferID" selected and I am creating a new record for every item
 For Each varItm In Me.ListBoxWaferID.ItemsSelected
   Me.tmpWaferID.value = Me.ListBoxWaferID.ItemData(varItm)
   varWaferID = Me.tmpWaferID
     
     Set rstWaferTable = db.OpenRecordset("tblWaferID", dbOpenDynaset, dbAppendOnly)
     With rstWaferTable
      .AddNew
            rstWaferTable![LotID] = varLotID
            rstWaferTable![WaferID] = varWaferID
      rstWaferTable.Update
      rstWaferTable.Close
      End With
 
'Right here I am putting the same "varWaferID"  in a seperate table
    Set rstLayerTable = db.OpenRecordset("tblWaferIDToLayer", dbOpenDynaset, dbAppendOnly)
    With rstLayerTable
      .AddNew
            rstLayerTable![WaferID] = varWaferID
      rstLayerTable.Update
      rstLayerTable.Close
      End With

'Right here I would like to append data from "listBoxLayerInfo" to the "varWaferID" created in "tblWaferIDToLayer".  
'There are about 6 columns in the listbox
'[Layer], [Field], [Address Size], [Mask Grade], and [Percent Clear]
''''''
''''''
''''''
''''''

   Next
0
Comment
Question by:lightcross
  • 9
  • 7
16 Comments
 
LVL 2

Expert Comment

by:rthomsen
ID: 9924566
I'm not sure if I understand your question but I think you are trying to figure out how to access the 6 columns in your other listbox to add it to the database.

Try using this
for index = 0 to lstbox.listcount -1
   If listbox.Selected(index) = True Then
       col1value = listbox.Column(0, index))
       col2value = listbox.Column(1, index))
       col3value = listbox.Column(2, index))
   End If
next index

I hope this helps
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924598
yes thanks.  i know how to access the columns however i dont know how to include it into my current code.  the column data needs to be included as you have it for each waferid added in table "tblWaferIDToLayer".
0
 
LVL 2

Expert Comment

by:rthomsen
ID: 9924656
Do you  need to get the ID generated from the database when you added the Record to the tblWaferIDToLayer table?

With rstLayerTable
      .AddNew
            rstLayerTable![WaferID] = varWaferID
            IDvariable = rstLayerTable.fields("GeneratedIDFieldName")
      rstLayerTable.Update
      rstLayerTable.Close
      End With

Otherwise why can it not be added using the same method as above with varwaferID variable?
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924844
ok, think of something like this
For Each varItm In Me.ListBoxWaferID.ItemsSelected
   Me.tmpWaferID.value = Me.ListBoxWaferID.ItemData(varItm)
   varWaferID = Me.tmpWaferID
     
     Set rstWaferTable = db.OpenRecordset("tblWaferID", dbOpenDynaset, dbAppendOnly)
     With rstWaferTable
      .AddNew
            rstWaferTable![LotID] = varLotID
            rstWaferTable![WaferID] = varWaferID
      rstWaferTable.Update
      rstWaferTable.Close
      End With
   
    Set rstLayerTable = db.OpenRecordset("tblWaferIDToLayer", dbOpenDynaset, dbAppendOnly)
    With rstLayerTable
      .AddNew
            rstLayerTable![WaferID] = varWaferID
For LayerIndex = 0 To ListBox.ListCount - 1
   If ListBox.Selected(Index) = True Then
       rstLayerTable![LayerNameRev] = listbox.Column(1, index))
       rstLayerTable![Field] = listbox.Column(2, index))
       rstLayerTable![AddressSize] = listbox.Column(3, index))
       rstLayerTable![MaskGrade] = listbox.Column(4, index))
       rstLayerTable![PercentClear] = listbox.Column(5, index))
   End If
Next LayerIndex
           
      rstLayerTable.Update
      rstLayerTable.Close
      End With
   Next
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924846
i understand what i just coded will not work.  but how can i make it work?
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924865
or maybe something more like this....

For Each varItm In Me.ListBoxWaferID.ItemsSelected
   Me.tmpWaferID.value = Me.ListBoxWaferID.ItemData(varItm)
   varWaferID = Me.tmpWaferID
     
     Set rstWaferTable = db.OpenRecordset("tblWaferID", dbOpenDynaset, dbAppendOnly)
     With rstWaferTable
      .AddNew
            rstWaferTable![LotID] = varLotID
            rstWaferTable![WaferID] = varWaferID
      rstWaferTable.Update
      rstWaferTable.Close
      End With
   
    Set rstLayerTable = db.OpenRecordset("tblWaferIDToLayer", dbOpenDynaset, dbAppendOnly)
    With rstLayerTable
      .AddNew
            rstLayerTable![WaferID] = varWaferID
For Each LayerIndex In Me.ListBox.ItemSelected
       rstLayerTable![LayerNameRev] = listbox.Column(1, index))
       rstLayerTable![Field] = listbox.Column(2, index))
       rstLayerTable![AddressSize] = listbox.Column(3, index))
       rstLayerTable![MaskGrade] = listbox.Column(4, index))
       rstLayerTable![PercentClear] = listbox.Column(5, index))
Next LayerIndex
           
      rstLayerTable.Update
      rstLayerTable.Close
      End With
   Next
0
 
LVL 2

Accepted Solution

by:
rthomsen earned 250 total points
ID: 9924867
For Each varItm In Me.ListBoxWaferID.ItemsSelected
   Me.tmpWaferID.value = Me.ListBoxWaferID.ItemData(varItm)
   varWaferID = Me.tmpWaferID
     
     Set rstWaferTable = db.OpenRecordset("tblWaferID", dbOpenDynaset, dbAppendOnly)
     With rstWaferTable
      .AddNew
            rstWaferTable![LotID] = varLotID
            rstWaferTable![WaferID] = varWaferID
      rstWaferTable.Update
      rstWaferTable.Close
      End With
   
    Set rstLayerTable = db.OpenRecordset("tblWaferIDToLayer", dbOpenDynaset, dbAppendOnly)
    With rstLayerTable
         For LayerIndex = 0 To ListBox.ListCount - 1
      If ListBox.Selected(Index) = True Then
        .AddNew
         rstLayerTable![WaferID] = varWaferID
         rstLayerTable![LayerNameRev] = listbox.Column(1, index))
         rstLayerTable![Field] = listbox.Column(2, index))
         rstLayerTable![AddressSize] = listbox.Column(3, index))
         rstLayerTable![MaskGrade] = listbox.Column(4, index))
         rstLayerTable![PercentClear] = listbox.Column(5, index))
         rstLayerTable.Update
      End If
   Next LayerIndex
            rstLayerTable.Close
      End With
Next
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924895
ok, now we are on to something.  

current problem is that it is only adding the first item in listbox rather then only the items that are selected.  
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 2

Expert Comment

by:rthomsen
ID: 9924923
I can't see why it wouldn't be adding all of the selected items in listbox.  You should set a break in the code and step through it using F8 to see why only 1 record is being added.
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924930
it is not adding only 1 record.  it is adding only the first record 10 times.  10 times is the number of total items in the listbox.
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924944
it appears to be having a problem with this:
rstLayerTable![LayerNameRev] = listbox.Column(1, index))


if i change it to this then it is okay.  but its the reason why its adding only the first item in listbox.  
rstLayerTable![LayerNameRev] = listbox.Column(1)

possibly there is something wrong with this:
 For LayerIndex = 0 To ListBox.ListCount - 1  
      If ListBox.Selected(Index) = True Then
0
 
LVL 2

Expert Comment

by:rthomsen
ID: 9924952
Here is the problem:

For LayerIndex = 0 To ListBox.ListCount - 1  
      If ListBox.Selected(Index) = True Then

You are incrementing a variable called Layerindex but using a variable called index to access the list.
0
 
LVL 2

Author Comment

by:lightcross
ID: 9924963
nope... i found the problems.  

(1)we somehow started interchanging "LayerIndex" and "Index"!    

(2)we had one too many parenthesis in our column code.

so here is the final which works perfectly!  thanks for all your help!  you did exactly what I wanted I just made things more difficult by trying to adjust your "index" to something more understandable to myself and neglected to implement it elsewhere.

For Each varItm In Me.ListBoxWaferID.ItemsSelected
   Me.tmpWaferID.value = Me.ListBoxWaferID.ItemData(varItm)
   varWaferID = Me.tmpWaferID
     
     Set rstWaferTable = db.OpenRecordset("tblWaferID", dbOpenDynaset, dbAppendOnly)
     With rstWaferTable
      .AddNew
            rstWaferTable![LotID] = varLotID
            rstWaferTable![WaferID] = varWaferID
      rstWaferTable.Update
      rstWaferTable.Close
      End With
   
    Set rstLayerTable = db.OpenRecordset("tblWaferIDToLayer", dbOpenDynaset, dbAppendOnly)
    With rstLayerTable
         For Index = 0 To ListBox.ListCount - 1
     
      If ListBox.Selected(Index) = True Then
        .AddNew
         rstLayerTable![WaferID] = varWaferID
         rstLayerTable![LayerNameRev] = ListBox.Column(1, Index)
         rstLayerTable![Field] = ListBox.Column(2, Index)
         rstLayerTable![AddressSize] = ListBox.Column(3, Index)
         rstLayerTable![MaskGrade] = ListBox.Column(4, Index)
         rstLayerTable![PercentClear] = ListBox.Column(5, Index)
         rstLayerTable.Update
      End If
   Next Index
            rstLayerTable.Close
      End With
Next
0
 
LVL 2

Expert Comment

by:rthomsen
ID: 9924977
Great!.  I'm happy it worked out for you.

Thanks

rthomsen
0
 
LVL 2

Author Comment

by:lightcross
ID: 9925001
i have one more question for you though...

how would i make it error proof if any one of the column values were null

         rstLayerTable![WaferID] = varWaferID
         rstLayerTable![LayerNameRev] = ListBox.Column(1, Index)
         rstLayerTable![Field] = ListBox.Column(2, Index)
         rstLayerTable![AddressSize] = ListBox.Column(3, Index)
         rstLayerTable![MaskGrade] = ListBox.Column(4, Index)  ''' <-------for instance, maybe this value is empty.  currently it errors out.  i would like it to continue to next
         rstLayerTable![PercentClear] = ListBox.Column(5, Index)
0
 
LVL 2

Expert Comment

by:rthomsen
ID: 9928688
The column property won't return null.  If it is blank it will return "", the empty string.  Make sure in your table you have the allowzerolength property set to true for all text fields.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

743 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

15 Experts available now in Live!

Get 1:1 Help Now