473,554 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Refresh Listbox from Other Form

Hi,
I've got a listbox on a form that gets updated by opening another form
for data entry. Once the user enters the new item name and clicks
"accept", the data entry form closes and the original form with the
listbox displays. I'm having trouble getting the listbox to reflect
the additional entry. If I close the form and reopen it, the new item
displays just fine, but I know that there is a more direct way to
force the listbox to refresh then closing and reopening. Can anybody
help?

Here is the code that I am using to get the listbox to refresh (but it
is obviously not working):

With frmJobsItems 'this is the name of the form with the
listbox on it
.bsJobs.Dispose () 'the listbox is tied to a binding source
.dsTimesheets.D ispose() 'this is the dataset that the
bindingsource pulls from
.taJobs.Fill(fr mJobsItems.dsTi mesheets.Jobs) 'this is the
tableadapter that holds the data
End With

Any help is appreciated.
Thanks,
Randy

Sep 23 '07 #1
7 2342
Create your method as public and start it from your other form.
Sorry, I don't understand how to implement this advice. Can anybody
tell me how to do this?

Thanks.

Sep 24 '07 #2
"Randy" <sp***********@ gmail.comschrie b
>
Create your method as public and start it from your other form.

Sorry, I don't understand how to implement this advice. Can anybody
tell me how to do this?

http://msdn2.microsoft.com/en-us/lib...a5(VS.80).aspx
Armin
Sep 24 '07 #3
Thanks, but I'm still stuck. I should have been more specific in my
reply. I know what public methods are. But in this case, there is no
method to make public. I have a listbox on form1 (which is bound to a
single column in a SQL table) and a textbox on form2. When the Accept
button on form2 is clicked, it initiates the code above, which submits
the text in the textbox into the SQL table and then attempts to
refresh the listbox from form1. Since the SQL table now includes this
new entry, the listbox will display it - if only I can trigger it to
refresh its databinding.

Sorry if I am being dense, but I am spinning my wheels over a couple
of lines of code.

Thanks again,
Randy

Sep 24 '07 #4
On Mon, 24 Sep 2007 12:02:39 -0700, Randy <sp***********@ gmail.com>
wrote:
>Thanks, but I'm still stuck. I should have been more specific in my
reply. I know what public methods are. But in this case, there is no
method to make public. I have a listbox on form1 (which is bound to a
single column in a SQL table) and a textbox on form2. When the Accept
button on form2 is clicked, it initiates the code above, which submits
the text in the textbox into the SQL table and then attempts to
refresh the listbox from form1. Since the SQL table now includes this
new entry, the listbox will display it - if only I can trigger it to
refresh its databinding.

Sorry if I am being dense, but I am spinning my wheels over a couple
of lines of code.
If I understand you correctly, this is the code that you are calling
from the Accept button on form2:

With frmJobsItems 'this is the name of the form with the
listbox on it
.bsJobs.Dispose () 'the listbox is tied to a binding source
.dsTimesheets.D ispose() 'this is the dataset that the
bindingsource pulls from
.taJobs.Fill(fr mJobsItems.dsTi mesheets.Jobs) 'this is the
tableadapter that holds the data
End With

First, frmJobItems is a variable containing a reference to form1. It
may coincidently be the name of the form, but the name does not
matter.

You Dispose the binding source and the dataset, then call the
TableAdapter passing a reference to something in the dataset that you
just disposed. You dispose something when you are finished using it,
not when you want to reuse it.

How are you modifying the table in form2? If you not doing the
modification by using dsTimesheets and taJobs, then you need to
refetch data into dsTimesheets.
Sep 24 '07 #5
Yes, frmJobItems is what I was referring to as form1 (just trying to
restate it simpler terms). I am using the dispose command simply as a
bad attempt to essentially "reboot" the listbox by closing its
bindingsource and then refilling the table adapter. Sorry if that is
a silly attempt, but I've tried many ways and this is simply the
latest bad attempt.

As for modifying the table, I didn't post that code since that part is
working fine and I was trying to keep this straightforward . Here is
the entire code (frmJobItems is "form1" which contains the listbox
that I am trying to refresh). It's only the last few lines of code
that don't work.

Private Sub btnAddJob_Click (ByVal sender As Object, ByVal e As
System.EventArg s) Handles btnAddJob.Click
Dim strSQL As String
strSQL = "SELECT JobName FROM Jobs WHERE JobName = @JobName"
Dim da As New SqlDataAdapter( strSQL, cn)
da.SelectComman d.Parameters.Ad dWithValue("@Jo bName",
tbNewJob.Text)
Dim tbl As New DataTable
da.Fill(tbl)
da.SelectComman d.Parameters.Cl ear()
If tbl.Rows.Count 0 Then 'this entry is already in the table
MessageBox.Show ("This job already exists in your list." &
vbCrLf & "Entry Aborted.", "Duplicate Entry Detected",
MessageBoxButto ns.OK, MessageBoxIcon. Stop)
Exit Sub
End If

If tbNewJob.Text = "" Then
MessageBox.Show ("Please enter a name for your job.",
"Invalid Entry", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
Exit Sub
End If

'Insert Recipe Header Row
strSQL = "INSERT INTO Jobs (JobName) VALUES (@JobName)"

Dim cmdInsert As New SqlCommand(strS QL, cn)
cmdInsert.Param eters.AddWithVa lue("@JobName", tbNewJob.Text)

Try
cn.Open()
cmdInsert.Execu teNonQuery()
cmdInsert.Param eters.Clear()
frmJobsItems.ta Jobs.Fill(dsTim esheets.Jobs)
Catch ex As Exception
MessageBox.Show (ex.Message)
cn.Close()
End Try

cn.Close()

With frmJobsItems
.bsJobs.Dispose ()
.dsTimesheets.D ispose()
.taJobs.Fill(fr mJobsItems.dsTi mesheets.Jobs)
End With

Me.Close()
End Sub

Thanks for considering this, Jack.

Randy

Sep 24 '07 #6
So it looks like you are not using form1's DataSet and DataAdapter to
update the table. In that case you need to reload the data into
form1's DataSet.

My guess is that would be something like:
With frmJobsItems
.taJobs.Fill(.d sTimesheets.Job s)
End With

That is exactly what you do in the try block to update
dsTimesheets.Jo bs, which I guess is a DataSet in from2. You could try
calling the listbox's Refresh() method, but I'm not sure why that
would be necessary.

Also, I would recommend creating a Sub in form1 to do all of this, and
calling that Sub from form2. It usually isn't a good idea to have
code someplace else (in form2 in this case) knowing about the names of
controls and objects in another object.

On Mon, 24 Sep 2007 15:07:49 -0700, Randy <sp***********@ gmail.com>
wrote:
>Yes, frmJobItems is what I was referring to as form1 (just trying to
restate it simpler terms). I am using the dispose command simply as a
bad attempt to essentially "reboot" the listbox by closing its
bindingsourc e and then refilling the table adapter. Sorry if that is
a silly attempt, but I've tried many ways and this is simply the
latest bad attempt.

As for modifying the table, I didn't post that code since that part is
working fine and I was trying to keep this straightforward . Here is
the entire code (frmJobItems is "form1" which contains the listbox
that I am trying to refresh). It's only the last few lines of code
that don't work.

Private Sub btnAddJob_Click (ByVal sender As Object, ByVal e As
System.EventAr gs) Handles btnAddJob.Click
Dim strSQL As String
strSQL = "SELECT JobName FROM Jobs WHERE JobName = @JobName"
Dim da As New SqlDataAdapter( strSQL, cn)
da.SelectComman d.Parameters.Ad dWithValue("@Jo bName",
tbNewJob.Tex t)
Dim tbl As New DataTable
da.Fill(tbl)
da.SelectComman d.Parameters.Cl ear()
If tbl.Rows.Count 0 Then 'this entry is already in the table
MessageBox.Show ("This job already exists in your list." &
vbCrLf & "Entry Aborted.", "Duplicate Entry Detected",
MessageBoxButt ons.OK, MessageBoxIcon. Stop)
Exit Sub
End If

If tbNewJob.Text = "" Then
MessageBox.Show ("Please enter a name for your job.",
"Invalid Entry", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
Exit Sub
End If

'Insert Recipe Header Row
strSQL = "INSERT INTO Jobs (JobName) VALUES (@JobName)"

Dim cmdInsert As New SqlCommand(strS QL, cn)
cmdInsert.Param eters.AddWithVa lue("@JobName", tbNewJob.Text)

Try
cn.Open()
cmdInsert.Execu teNonQuery()
cmdInsert.Param eters.Clear()
frmJobsItems.ta Jobs.Fill(dsTim esheets.Jobs)
Catch ex As Exception
MessageBox.Show (ex.Message)
cn.Close()
End Try

cn.Close()

With frmJobsItems
.bsJobs.Dispose ()
.dsTimesheets.D ispose()
.taJobs.Fill(fr mJobsItems.dsTi mesheets.Jobs)
End With

Me.Close()
End Sub

Thanks for considering this, Jack.

Randy
Sep 25 '07 #7
That was it. I simply had to create a public sub on form1 containing
one line of code to fill the table adapter and call it from form2.
This was probably what Cor was suggesting, but I wasn't smart enough
to implement it.

Thanks to everybody who replied, particularly Jack.
Randy

Sep 27 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2480
by: Andrew Chanter | last post by:
I have a situation where I am using an unbound dialog form to update data in an Access 2002 split back end / front end scenario. The data update is done via an ADO call (direct to the back end db) when the user clicks the save button. The dialog then closes and the user should be able to see the result of their edit in a list view that now...
4
5634
by: news.shaw.net | last post by:
How do I make a subform change to reflect a changed query which is the forms Source Object. Another part of the application has changed the query via VB, and I can see that the query is how I want it, but the subform hasn't picked up on the changes. Refresh doen't work RefreshDatabaseWindow doesn't work
2
2920
by: S. van Beek | last post by:
Dear reader, I have a strange situation. In a Form with a list box to search for a record in the form it self. The field in the list box is also available in the form. On an other form I
3
7850
by: Brian Basquille | last post by:
Hey there all, Fairly easy question for a C# expert, i assume! Am still working on my Photo Album. On frmBrowsePhotos, i have a listBox (photosList) containing photoID's from a table in an Access Database. I click 'Add Photo' on that form and after the OpenFileDialog (etc.), it asks me to save some information about the photo (caption...
6
5191
by: Janaka | last post by:
Help! I have two ListBox controls on my web form. The first one gets populated on entry with values from the DB. I then use JavaScript to copy options from this ListBox to my second one. (I have also tried changing the second ListBox to an HtmlSelect control) using bog standard JavaScript code like so where "used" is the name of my...
0
860
by: Kele | last post by:
I have a web form that users populate a list box and textboxes and submit the form. I have the form in a placeholder on the page and a summary of what was entered when they hit 'Submit' in another placeholder. When they hit 'Submit' the form placeholder is hidden and the summary placeholder is shown along with the information in the form...
5
20153
by: Randy | last post by:
I have button that deletes items from a SQL datatable. The items are displayed for the user in listbox, which is bound to the dataset. The code works just fine insofar as it deletes the correct record from the dataset and the datasource. What I need to make it do is to refresh the listbox so that it reflects that the record has been...
5
2457
by: BlackBox | last post by:
I have a Listbox1 in the main form and I can add listbox1 items in the parent form. But when I close the parent form Listbox1 in the main form does not get refresh. I need to re-open the app. Any idea on how to refresh items in the main from from a parent form without opening the app. -Thanks
45
6830
by: angelicdevil | last post by:
i have 2 tables 1 is status_type with field name status and other is users with field username and status now i want that the first listbox lists all status from status type ( this i have achieved with php ) and based on selection of the value in listbox 1 the corresponding data in table users be loaded in listbox 2 . i have achieved...
0
7530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8047
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6156
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5441
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5162
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3556
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.