Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default listbox -shows table of information - option to select and edit ro

I have a listbox which displays a table of chemicals (1st column) and their
properties - density (2nd column) and other propeties (3rd, 4th, 5th column
etc)

When I open the userform the table of chemicals is displayed and i can
select a row of chemicals (highlighted in blue).

I already have another userform which allows me to enter new chemicals to
add to the list and I have txt boxes for entering the chemical data

Here is the code i use for that

Private Sub cmdAdd_Click()
Application.EnableEvents = False
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Chemicals")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row


'check for a part number
If Trim(Me.txtChemical.Value) = "" Then
Me.txtChemical.SetFocus
MsgBox "Please enter a chemical name"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtChemical.Value
ws.Cells(iRow, 2).Value = Me.txtCAS.Value
ws.Cells(iRow, 3).Value = Me.txtMW.Value
ws.Cells(iRow, 4).Value = Me.txtDensity.Value
ws.Cells(iRow, 5).Value = Me.txtMP.Value
ws.Cells(iRow, 6).Value = Me.txtBP.Value
ws.Cells(iRow, 7).Value = Me.txtFP.Value
ws.Cells(iRow, 8).Value = Me.txtMSDS.Value

'clear the data
Me.txtChemical.Value = ""
Me.txtCAS.Value = ""
Me.txtMW.Value = ""
Me.txtDensity.Value = ""
Me.txtMP.Value = ""
Me.txtBP.Value = ""
Me.txtFP.Value = ""
Me.txtMSDS.Value = ""


Me.txtChemical.SetFocus
'Application.EnableEvents = True
End Sub

What I am looking to do on the first userform (which displays all the
chemicals is to select a row and have that data pulled to the different text
boxes so that the individual properties can be edited and the table updated.

Can anyone help me with this or dierct me to a link whereby a table of data
can be appended to and/or edited.

regards,

Roger


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default listbox -shows table of information - option to select and editro

On 29 Nov, 00:24, Roger on Excel
wrote:
I have a listbox which displays a table of chemicals (1st column) and their
properties - density (2nd column) and other propeties (3rd, 4th, 5th column
etc)

When I open the userform the table of chemicals is displayed and i can
select a row of chemicals (highlighted in blue).

I already have another userform which allows me to enter new chemicals to
add to the list and I have txt boxes for entering the chemical data

Here is the code i use for that

Private Sub cmdAdd_Click()
Application.EnableEvents = False
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Chemicals")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
* .End(xlUp).Offset(1, 0).Row

'check for a part number
If Trim(Me.txtChemical.Value) = "" Then
* Me.txtChemical.SetFocus
* MsgBox "Please enter a chemical name"
* Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtChemical.Value
ws.Cells(iRow, 2).Value = Me.txtCAS.Value
ws.Cells(iRow, 3).Value = Me.txtMW.Value
ws.Cells(iRow, 4).Value = Me.txtDensity.Value
ws.Cells(iRow, 5).Value = Me.txtMP.Value
ws.Cells(iRow, 6).Value = Me.txtBP.Value
ws.Cells(iRow, 7).Value = Me.txtFP.Value
ws.Cells(iRow, 8).Value = Me.txtMSDS.Value

'clear the data
Me.txtChemical.Value = ""
Me.txtCAS.Value = ""
Me.txtMW.Value = ""
Me.txtDensity.Value = ""
Me.txtMP.Value = ""
Me.txtBP.Value = ""
Me.txtFP.Value = ""
Me.txtMSDS.Value = ""

Me.txtChemical.SetFocus
'Application.EnableEvents = True
End Sub

What I am looking to do on the first userform (which displays all the
chemicals is to select a row and have that data pulled to the different text
boxes so that the individual properties can be edited and the table updated.

Can anyone help me with this or dierct me to a link whereby a table of data
can be appended to and/or edited.

regards,

Roger


Hi Roger,

If I understand your problem correctly you want to edit a chemical so
that the data behind is updated?

If so what I tend to do is use the listbox double click event, from
this get the data from the list box and populate the text boxes of the
form:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' where userform2 is the name of the form containing the textboxes
userform2.txtChemical = Me.ListBox1.Column(0,
Me.ListBox1.ListIndex)
userform2.txtCAS = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)
' And so on
End Sub

that you use to add a new chemical as its easier to reuse the form,
there only two changes you will have to do, firstly change iRow but to
do this you will need a flag on your userform2 form to tell it whether
the form is adding a chemical or editing a chemical, the easiest way
to do this is to add a text box on the form and change the textbox
property to visible=false, in the listbox double click event also
populate the hidden textbox with say "EDIT" therefore you can then use
this in an if statement for iRow, something like:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' where userform2 is the name of the form containing the textboxes
userform2.txtChemical = Me.ListBox1.Column(0,
Me.ListBox1.ListIndex)
userform2.txtCAS = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)
' And so on
userform2.txtHiddenTextBox = "EDIT"
End Sub


Private Sub cmdAdd_Click()
Application.EnableEvents = False
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Chemicals")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row


If me.hiddentextbox = "EDIT" then
iRow = application.match(me.txtChemical, ws.range("A:A"),0)
else
iRow = iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
end if

The rest of your code should just then work, normally I would test the
match to see if it returns an error but as you are checking against
the source data of the form, it can never not exist.

Any problems let me know,

James
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default listbox -shows table of information - option to select and editro

On 29 Nov, 20:34, Billy wrote:
On 29 Nov, 00:24, Roger on Excel





wrote:
I have a listbox which displays a table of chemicals (1st column) and their
properties - density (2nd column) and other propeties (3rd, 4th, 5th column
etc)


When I open the userform the table of chemicals is displayed and i can
select a row of chemicals (highlighted in blue).


I already have another userform which allows me to enter new chemicals to
add to the list and I have txt boxes for entering the chemical data


Here is the code i use for that


Private Sub cmdAdd_Click()
Application.EnableEvents = False
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Chemicals")


'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
* .End(xlUp).Offset(1, 0).Row


'check for a part number
If Trim(Me.txtChemical.Value) = "" Then
* Me.txtChemical.SetFocus
* MsgBox "Please enter a chemical name"
* Exit Sub
End If


'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtChemical.Value
ws.Cells(iRow, 2).Value = Me.txtCAS.Value
ws.Cells(iRow, 3).Value = Me.txtMW.Value
ws.Cells(iRow, 4).Value = Me.txtDensity.Value
ws.Cells(iRow, 5).Value = Me.txtMP.Value
ws.Cells(iRow, 6).Value = Me.txtBP.Value
ws.Cells(iRow, 7).Value = Me.txtFP.Value
ws.Cells(iRow, 8).Value = Me.txtMSDS.Value


'clear the data
Me.txtChemical.Value = ""
Me.txtCAS.Value = ""
Me.txtMW.Value = ""
Me.txtDensity.Value = ""
Me.txtMP.Value = ""
Me.txtBP.Value = ""
Me.txtFP.Value = ""
Me.txtMSDS.Value = ""


Me.txtChemical.SetFocus
'Application.EnableEvents = True
End Sub


What I am looking to do on the first userform (which displays all the
chemicals is to select a row and have that data pulled to the different text
boxes so that the individual properties can be edited and the table updated.


Can anyone help me with this or dierct me to a link whereby a table of data
can be appended to and/or edited.


regards,


Roger


Hi Roger,

If I understand your problem correctly you want to edit a chemical so
that the data behind is updated?

If so what I tend to do is use the listbox double click event, from
this get the data from the list box and populate the text boxes of the
form:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' * where userform2 is the name of the form containing the textboxes
* * userform2.txtChemical = Me.ListBox1.Column(0,
Me.ListBox1.ListIndex)
* * userform2.txtCAS = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)
' * And so on
End Sub

that you use to add a new chemical as its easier to reuse the form,
there only two changes you will have to do, firstly change iRow but to
do this you will need a flag on your userform2 form to tell it whether
the form is adding a chemical or editing a chemical, the easiest way
to do this is to add a text box on the form and change the textbox
property to visible=false, in the listbox double click event also
populate the hidden textbox with say "EDIT" therefore you can then use
this in an if statement for iRow, something like:

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' * where userform2 is the name of the form containing the textboxes
* * userform2.txtChemical = Me.ListBox1.Column(0,
Me.ListBox1.ListIndex)
* * userform2.txtCAS = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)
' * And so on
* * userform2.txtHiddenTextBox = "EDIT"
End Sub

Private Sub cmdAdd_Click()
Application.EnableEvents = False
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Chemicals")


'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
* .End(xlUp).Offset(1, 0).Row


If me.hiddentextbox = "EDIT" then
* * iRow = application.match(me.txtChemical, ws.range("A:A"),0)
else
* * iRow = iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
end if

The rest of your code should just then work, normally I would test the
match to see if it returns an error but as you are checking against
the source data of the form, it can never not exist.

Any problems let me know,

James


Hi Roger,

Just reread my post and the bottom code should be the if statement:

If me.hiddentextbox = "EDIT" then
iRow = application.match(me.txtChemical, ws.range("A:A"),0)
else
iRow = iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
end if


replaces your original iRow statement:

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row


It will work either way but if its left in it will be less efficient.

James
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pivot Table filter--can the select (All) option be eliminated? MichelleH Excel Discussion (Misc queries) 0 March 15th 10 03:13 PM
macro to select option button in option group cm Excel Programming 2 October 20th 09 04:49 PM
Select & edit items in a listbox in VBA poppy Excel Programming 6 May 12th 06 01:10 PM
SELECT, ERASE, EDIT items in listbox unplugs[_29_] Excel Programming 10 June 30th 04 04:56 AM
Select from table and listbox PawelR Excel Programming 1 November 7th 03 01:21 PM


All times are GMT +1. The time now is 04:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"