473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

form based on a query

19
Hello

I've got a form which is based on a query

It's opened by a command button - when the command button is clicked a tect box prompts for the criteria "enter word"

The query searches a list of courses for any containing the "word" and then the form opens and displays those records

So far so good - BUT
if there are no matches for the "word" - the form opens but is just a grey screen - no boxes or anything - just blank

I've tried code on the command button and on the form itself - I'd just like a message box saying "no records to display" and then for the form to close and the previous screen (with the command button on it) to reappear

I've even tried putting some extra code on the SQL of the query - but having absolutely no luck

I can get a message box to display if there is a record with the following code:
Private Sub Form_Load()
If [course name] <> "" Then
MsgBox "is record"

End If
End Sub

and I tried
Else
MsgBox "no record"
but that didn't work

a message box on the "on error" of the form load does come up - but it comes up whether the query returns something or not

stumped - any help greatfully received
thanks
Shona
Sep 11 '07 #1
18 2155
istya
35
I had this problem, what you can do is
Expand|Select|Wrap|Line Numbers
  1. dim dbs as dao.database
  2. dim results as dao.recordset
  3. set dbs = currentdb
  4. set results = dbs.OpenRecordset("SELECT * FROM query;")
  5. if results.recordcount = 0 then
  6.     msgbox("There were no records to display")
  7. else
  8.     'open form
  9. end if
  10.  
where query is the query that you form is based on.
If you put this in the Form_Load() sub it should work.
Sep 11 '07 #2
istya
35
Sorry, one thing I for to say, if you use "SELECT COUNT(*) FROM query;" then recordcount will ALWAYS be 1 as you have a result - its a value of 0, but it's a result. If you use the count function, you can use result(0) instead of results.recordcount as results(0) will use the value returned by "SELECT COUNT(*) FROM query;"
Sep 11 '07 #3
shonam
19
thanks very much - I gave it a try but get
runtime error 3061
too few parameters. Expected 1
on the line:
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")

not sure what it means
Shona
Sep 11 '07 #4
istya
35
Can you do me a favour and post the whole sub in and I'll see if I can find the problem.
Sep 11 '07 #5
istya
35
One other question - do you have Microsoft DAO ticked in your "Tools->References" in the VBA??

I think I know what the problem is -

Expand|Select|Wrap|Line Numbers
  1.     Dim dbs As DAO.Database
  2.     Dim results As DAO.Recordset
  3.  
rather than
Expand|Select|Wrap|Line Numbers
  1.     Dim dbs As DOA.Database
  2.     Dim results As DOA.Recordset
  3.  
My mistake
Sep 11 '07 #6
shonam
19
I noticed the doa / dao thing and changed it - and I checked that the dao is checked in references

the sub is:

Private Sub Form_Load()
Dim dbs As DAO.Database
Dim results As DAO.Recordset
Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
If results(0) Then
MsgBox ("There were no records to display")
Else
'open form
End If

should I have changed anything apart from the query name
(sorry - really rusty on Access and trying to re-learn)

thanks again
Shona

End Sub
Sep 11 '07 #7
istya
35
you want to use results.recordcount rather than results(0) as you don't have count in the recordset sql string

Expand|Select|Wrap|Line Numbers
  1. If results.recordcount = 0 Then
  2.    MsgBox ("There were no records to display")
  3. Else
  4.    'open form
  5. End If
  6.  
Sep 11 '07 #8
shonam
19
Still getting a grey screen I'm afraid - code now says:

Private Sub Form_Load()
Dim dbs As DAO.Database
Dim results As DAO.Recordset
Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
If results.RecordCount = 0 Then
MsgBox ("There were no records to display")
Else
'open form
End If

End Sub
Sep 11 '07 #9
istya
35
does it display the "There were no records to display" message or doesn't it get that far?
Sep 11 '07 #10
shonam
19
if there are records then the form opens and displays them properly - if there are no records I just get the grey screen - the message box doesn't appear
Sep 11 '07 #11
istya
35
What I suggest you do then is the code for the command button rather than the form_load() sub

Expand|Select|Wrap|Line Numbers
  1. Private Sub command_button_click()
  2. Dim dbs As DAO.Database
  3. Dim results As DAO.Recordset
  4. Set dbs = CurrentDb
  5. Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
  6. If results.recordcount = 0 Then
  7. MsgBox ("There were no records to display")
  8. Else
  9. 'open form
  10. End If
  11.  
This means that if there are no records to display it won't open the form. Alternatively, you can
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim dbs As DAO.Database
  3. Dim results As DAO.Recordset
  4. Set dbs = CurrentDb
  5. Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
  6. If results.recordcount = 0 Then
  7. MsgBox ("There were no records to display")
  8. docmd.close
  9. End If
  10.  
which should close the form if there are no reocrds to display.
Sep 11 '07 #12
shonam
19
tried both of those but I'm afraid I get
runtime error 3061
too few parameters. Expected 1

sorry and thanks for helping - I'm way out of my depth here
Sep 11 '07 #13
istya
35
post your code and I'll have a look. Also, let me know if you want to open the form and then check or check than open the form if required.
Sep 11 '07 #14
shonam
19
I just pasted in the code you suggested
tried both ways as I don't mind whether the opens or not really - either way is fine
Sep 11 '07 #15
istya
35
If you do the working around the command button rather than form_load where formname is the name of the form that you are trying to open and command_button is the name of you button

Expand|Select|Wrap|Line Numbers
  1. Private Sub command_button_click()
  2.  
  3. Dim dbs As DAO.Database
  4. Dim results As DAO.Recordset
  5. dim stdocname as string
  6. dim Dim stLinkCriteria As String
  7.  
  8. Set dbs = CurrentDb
  9. Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
  10.  
  11. If results.recordcount = 0 Then
  12.    MsgBox ("There were no records to display")
  13. Else
  14.    stDocName = "formname"
  15.    DoCmd.OpenForm stDocName, , , stLinkCriteria
  16. End If
  17.  
  18. End Sub
  19.  
If this still doesn't work, post your code and I'll see what else I've got wrong!!!
Sep 11 '07 #16
shonam
19
am getting the runtime error as soon as I press the command button - the text box which should prompt for the "word" doesn't appear

code is:

Private Sub viewtr_Click()

Dim dbs As DAO.Database
Dim results As DAO.Recordset
Dim stdocname As String
Dim stLinkCriteria As String

Set dbs = CurrentDb
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")

If results.RecordCount = 0 Then
MsgBox ("There were no records to display")
Else
stdocname = "seltrain"
DoCmd.OpenForm stdocname, , , stLinkCriteria
End If

End Sub

the line:
Set results = dbs.OpenRecordset("SELECT * FROM selcour;")
is highlighted in the debug window

you are a very patient person - thank you
Sep 11 '07 #17
shonam
19
Hello again

I've been trawling the internet to see if anyone else has asked about this problem before and found the following code which goes on the Form Load event:


If Me.RecordSetClone.RecordCount = 0 Then
DoCmd.Close
End If

and it seems to work beautifully - even though I have no idea what it's doing

thank you so very much for all your help - I have learned a great deal this morning

very best wishes
Shona
Sep 11 '07 #18
Hi Shona

I cannot help you with you question, but would like to source some info from you - What you have described when opening your form seems to be exactly what I am trying to do (hence my posting on bound commands). but cannot get right.

How did you build and link the command button and the text box? - I would dearly like to do the same but have no idea how - And then maybe in my text box provide the option of a dropdown box to select from.

Can you PLEASE PLEASE tell me what you did?

Annette

Hello

I've got a form which is based on a query

It's opened by a command button - when the command button is clicked a tect box prompts for the criteria "enter word"

The query searches a list of courses for any containing the "word" and then the form opens and displays those records

So far so good - BUT
if there are no matches for the "word" - the form opens but is just a grey screen - no boxes or anything - just blank

I've tried code on the command button and on the form itself - I'd just like a message box saying "no records to display" and then for the form to close and the previous screen (with the command button on it) to reappear

I've even tried putting some extra code on the SQL of the query - but having absolutely no luck

I can get a message box to display if there is a record with the following code:
Private Sub Form_Load()
If [course name] <> "" Then
MsgBox "is record"

End If
End Sub

and I tried
Else
MsgBox "no record"
but that didn't work

a message box on the "on error" of the form load does come up - but it comes up whether the query returns something or not

stumped - any help greatfully received
thanks
Shona
Sep 12 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John | last post by:
Just a general question... I'm currently using a combobox that when updated, opens a form with its recordset based on a query using the combo box value as the criteria. I'm I correct in...
2
by: neptune | last post by:
I have a query where each customer has an or . Sometimes both fields for a customer are populated, but if is null, then will be populated and vice versa. I have a form, , where I select a...
4
by: DebbieG | last post by:
I have a form based on this query: SELECT Students.LastSerDT, OtherInfo.Served, OtherInfo.HSGradYr, OtherInfo.ActivePart, OtherInfo.Served, Students.SSN, & ", " & & " " & AS Name,...
2
by: dkintheuk | last post by:
Hi all, Using Access 2000 on XP Pro PC. I have a form that is based on a presaved query - all fine with this. I also have a refresh button that takes the values from various unbound...
1
by: meganrobertson22 | last post by:
hi everybody- what is the best way to add data from one form to another? i have 2 tables: person and contract. here are some of the fields. table: person personid (autonumber and primary...
2
by: Robert | last post by:
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...
2
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
4
by: john | last post by:
I created a form via the wizard with 1 main table and 2 one-on-one tables. As i couldn't add a field to the form (a field that I added to the table after creating the form), I googled out that the...
31
by: DWolff | last post by:
I'm somewhat new to Access, and have no VB experience at all (except for modifying existing code where obvious). I built a data entry form that works fine. Typically, a client will call in and...
8
by: tess | last post by:
I have: table 1 - tblLeadInfo which includes a salesman ID field table 2 - tbllkpSalesman with all zips in the state and a Salesman assigned to that area. I have a form based on table #1 When...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.