473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I make a form re-read (refresh) it's datasource

I am trying to give the user dynamic search capabilities to select
almost any record in the database from criteria they select. Everything
seems to work except when I open the display form to display the data
to the user. If that form is already open, how do I make it refresh its
data source and display the new data.

Here is what I am doing.
From a text entry form, I create an SQL query dymanically and alter the

sql statement of an existing query or create a new query. Then I open
the display form (F2) with this new query as the recordsource. It works
if the display form (F2) is not open. But if that form is already open
displaying filtered results, it doesn't refresh the data based on the
updated query.

Sample code

Dim myCriteria As String, MyRecordSource As Variant
Dim MyQueryDef As DAO.QueryDef
Dim db As DAO.Database
Dim ArgCount As Integer
Dim Encode As Boolean
Dim strfield As String, S2Qry As String, sqlQ10 As String, sqlQ11
As String
Dim rst As DAO.Recordset
Set db = CurrentDb()

If S2Qry = "" Then
S2Qry = "s2_" & CurrentUser()
End If
'
' Initialize variables.
'
ArgCount = 0
myCriteria = ""
MyRecordSource = ""
'
' Use values entered in text boxes in form header to create
criteria for WHERE clause.
' The AddToWhere function constructs syntacticly corrent SQL where
clause based on input
'
AddToWhere [Id], "[tbl4].Id", myCriteria, ArgCount,
[btn_exact_match]
AddToWhere [Name], "[tbl4].Name", myCriteria, ArgCount,
[btn_exact_match]
AddToWhere [Desc], "[tbl4].Desc", myCriteria, ArgCount,
[btn_exact_match]
'
' If no criterion specifed, return all records.
'
If myCriteria = "" Then
myCriteria = "True"
End If
CallSource = Me.Name
'
' Open the Word Definition Sheet with search criteria specified
'
MyRecordSource = sqlQ10 & " where " & myCriteria & ";"

At this point MyRecordSource contains a valid SQL query statement that
selects
the records the user wants to see in the form. Set the form
recordsource to display
the data.

'
' The function ObjectExists returns true if the item exists
'
If ObjectExists("q ueries", S2Qry) = True Then 'the query
exists, modify it
'
' modify the SQL property of the existing query
'
Set MyQueryDef = db.QueryDefs(S2 Qry)
MyQueryDef.sql = MyRecordSource
db.QueryDefs.Re fresh
Else ' the query does not exist, create it
Set MyQueryDef = db.CreateQueryD ef(S2Qry, MyRecordSource)
db.QueryDefs.Re fresh
End If
'
' If the form is open, then refresh the recordsource to update the
new
' query data
'
If SysCmd(acSysCmd GetObjectState, acForm, "f2") = 1 Then
Form.RecordSour ce = Form_f2.RecordS ource
Else
'
' set the form recordsource to the query and open it.
'
Form_f2.RecordS ource = S2Qry
DoCmd.OpenForm strF2
End If
So, when I execute, if the form F2 is not open, the form opens and
displays the correct queried data. I use a debug.print me.recordsource
in the form_open() procedure and I know that the recordsource is set to
S2Qry. The form displays only those records that match the query
selection. Opening the query in the database window shows only records
matching the query.

Now, then I rerun the query with a new search, the querydef exists, so
I modify the sql statement and refresh the query (I can see that new
data is selected by opening the query in the database window) but the
form doesn't refresh with the new data. It still displays the old query
results. Unless I close the form and reopen it, the new query results
aren't displayed. How can I make the form re-read the recordsource to
get the new query data?

May 19 '06 #1
2 8176
Hi Robert.

Use Me.refresh or Me.requery after the line where you change the form's
datasource.

HTH

Colin

*** Sent via Developersdex http://www.developersdex.com ***
May 19 '06 #2
Colin,

Thanks for the advice and I have done that. First, the procedure above
is not in the form so Me.refresh won't work. You will notice that I do
have a requery on the querydef and I have put me.requery and me.refresh
in the on_current() procedure in the form. It still doesn't work. If I
close the form and the reopen, it always pulls the correct data set. I
just wanted a way to do it without closing the form. Procedures from
the forms are below.
Private Sub Form_Current()
Dim stEncWrd As Boolean
Dim sttext As String
'
' Set the input focus to the correct tab based on word type
'
stEncWrd = Me![WrdEncode]
Me.Requery
Debug.Print (Me.RecordSourc e)

If stEncWrd Then
Me![EncWrd].SetFocus
Else
Me![StdWrd].SetFocus
End If

End Sub
Private Sub Form_Load()
Select Case CallSource
Case "main_menu"
Me.RecordSource = defF2rst
Me.Requery
Case "sf8_msg_wordid "

Case "s2_wrd_definit ion_sheet"
Me.RecordSource = S2Qry
Me.Requery
Case Else
Me.RecordSource = defF2rst
Me.Requery
End Select

End Sub

I have noticed that sometimes it does work. It just isn't consistent.

Anyother suggestions?

May 19 '06 #3

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

Similar topics

4
9983
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when performing the validation check on the values that were entered into the form, I simply repost the form to perform the PHP validation. If any of the values that have been entered into the form are incorrect, I display a warning message on the screen...
4
2372
by: Andrew Williams | last post by:
Does anyone know of a way to quickly validate an HTML web form? I need to make sure that certain fields on my form actually contain data and are not submitted blank. I have tried using client-side Javascript validation which flags up when a field is empty but the form is still submitted. Here is the ASP code I am using to submit the form to an e- mail with CDONTS:
2
2187
by: bart plessers | last post by:
Hello, I have a form with some checkboxes, i.e. The default value of this checkbox is determined in a global file (config.asp), that is included in first line of the form, i.e. ShowFilenames = 1 The first time the form is displayed, the value of ShowFilenames should be
1
1692
by: sean | last post by:
HI There, I am trying to total the value of some form fields I am referecing the form field with an inner loop, I am having a little trouble because the amount of fields returned is always different, how can I roll-up the total of the form fields (request.form("subtotal_"& i)) to get one final figure? Sean - Thanks in advance for your answer
2
2907
by: Paul | last post by:
Hi this is related to a previous post, hopefully just a bit clearer description o the problem. I have a parent form that opens a new form (child form) while still leaving the parent form open. Although the child form has a close button (with java script) if the user navigates the parent form to another form the child form is still open. Just wondering if there is a way to close the child form when the user navigates away with the parent...
4
3768
by: Michael Kujawa | last post by:
I am using the following to create an SQL statement using the names and values from request.form. The loop goes through each item in request.form The issue comes in having an additional "and" at the end of the loop and the value of x3 as not all options may be selected from the form yet the loop goes through the entire request.form list I have to add addtional code to strip off the last "and" and was wondering if there is a way to...
2
2134
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. Using Safari,my first iteration the script works fine ( indicating that there are 33 form variables ). When trying another dropdown select value, the
6
6199
ak1dnar
by: ak1dnar | last post by:
I have created HTML form and a Java script to validate the form. Here is my requirement. When user select one from any list menu others should set to ALL. Its working here but the problem is we will say that user will first select one from Brand as IBM then he will select Type as Laptop. then what will happen again brand will set to All. I need to avoid happening this.. again i want to repeat the same for all the other menus. please be...
7
2091
by: sbryguy | last post by:
Greetings, I'm pretty new at ASP/SQL so if this seems like a no brainer, please enlighten me. I have a form that is being used to track volunteer activities for employees in my company. The form has multiple input fields with the same id in case the employee has volunteered for multiple organizations. First of all what's the beset practice out there for handling this scenario? Currently I'm running a FOR loop which is creating a new...
4
1949
by: gblack301 | last post by:
Hi, I have a search form where the user can check a box or enter some data such as a name to quey the database. I was wondering what is the best way to enable the ability for a user data in more than one field or check muliple boxes to query the database. What I want to do is create a multiple search criteria data string. Below is the code that working with a the moment. Any input would be appreciate. Thanks Greg If...
0
9591
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
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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
7415
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
6676
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
5312
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
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.