473,698 Members | 2,166 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 2347
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
2492
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 has focus. This is where I run into a problem. The list view wont refresh to pick up the data...
4
5641
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
2929
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
7854
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 etc.) on a new form (frmAddPhotoInfo) opened up in front of the previous (frmBrowsePhotos).
6
5212
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 <select> control: used.options = new Option(name, typeId); This all works fine and I can move across...
0
862
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 is cleared. When the user hits 'Back' in Internet Explorer they get the 'Warning page
5
20184
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 deleted. Right now, the listbox removes the first item from the list and leaves the deleted item...
5
2468
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
6865
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 this by using a button in php but i want tht it operate without submit button and no page refreshing ...
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6521
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.