473,386 Members | 1,699 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,386 software developers and data experts.

Running a Query from a form

Hi
I am new at setting up a database and requesting info from groups so
please excuse any mistakes that I inadvertently make.

I have set up a table with client info including a ClientID. The
ClientID is made up of the first letter of the client name plus a
sequential number eg A10 (Adam Bros P/L) J03 (Jolly, TJ & Sons) and I
want it calculated for me so as to minimise mistakes.

I set up a form to enter the client info & included a textbox (Control
Source)=Left$([Client],1). I then figured if I ran a query
automatically with the SQL statement SELECT Count ([ClientID]) AS
NewNumber FROM Clients WHERE [ClientID] Like 'A??'; that would work
out how many clients started with that letter then the ClientID
control would be =Left$([Client],1) & Sum([Query1]![NewNumber]+1).

What I don't know how to do is run the query from the form
automatically, how to set up a variable to collect the letter from the
textbox and if I am doing it correctly in the first place.
Nov 13 '05 #1
5 1704
The Count won't work, because if you delete Client A8, that could would give
you A10 again even though you already have an A10.

Why not just use an autonumber? Saves all the hassles, and solves the
multi-user issues. For example, if you have 2 users adding new clients
starting with A at once, your own code is likely to give them the same key.
To solve that properly means writing code to handling the locking issues,
and introduce random delays and retries to overcome the clash. The
AutoNumber solves this for you.

If you really want to use your code your own, you need to use codes like
A000001 so you can quickly identify the maximum value assigned to the prefix
letter, increment it, and create the new key. Use the BeforeUpdate event
procedure of your form, since that is the last possible moment before the
record is saved.

The kind of thing you will want is:
Left([ClientName], 1) & Format(Val(Nz(DMax("ClientID", "Clients" , "ClientID
Like """ & Left([ClientName], 1) & "*"""), 0)) + 1, "000000")

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Rupert Street" <ja**********@yahoo.com.au> wrote in message
news:3a*************************@posting.google.co m...
Hi
I am new at setting up a database and requesting info from groups so
please excuse any mistakes that I inadvertently make.

I have set up a table with client info including a ClientID. The
ClientID is made up of the first letter of the client name plus a
sequential number eg A10 (Adam Bros P/L) J03 (Jolly, TJ & Sons) and I
want it calculated for me so as to minimise mistakes.

I set up a form to enter the client info & included a textbox (Control
Source)=Left$([Client],1). I then figured if I ran a query
automatically with the SQL statement SELECT Count ([ClientID]) AS
NewNumber FROM Clients WHERE [ClientID] Like 'A??'; that would work
out how many clients started with that letter then the ClientID
control would be =Left$([Client],1) & Sum([Query1]![NewNumber]+1).

What I don't know how to do is run the query from the form
automatically, how to set up a variable to collect the letter from the
textbox and if I am doing it correctly in the first place.

Nov 13 '05 #2
Thankyou for your reply

I am not sure that autonumber would work and I am not sure that I
explained the numbering correctly. If I have 10 'A' clients the next
one would be A11 then if the next client was a 'P' and there were 3
existing 'P's the next one would be P4.

Clients aren't deleted even though they might no longer exist and only 1
person has access to set up a new client so that gets around the
deletion and locking issues.

I am presuming that Val = value but I couldn't figure out what Nz meant
(I could use some humour here but I won't).

Yours R
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
Nz() converts a Null to a zero or other value.
Val() converts a string to a number.
DMax() gets the maximum used so far.
Left() takes the left characters of a field, and Mid() some of the
characters from the middle.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Rupert Street" <ja**********@yahoo.com.au> wrote in message
news:41**********************@news.newsgroups.ws.. .
Thankyou for your reply

I am not sure that autonumber would work and I am not sure that I
explained the numbering correctly. If I have 10 'A' clients the next
one would be A11 then if the next client was a 'P' and there were 3
existing 'P's the next one would be P4.

Clients aren't deleted even though they might no longer exist and only 1
person has access to set up a new client so that gets around the
deletion and locking issues.

I am presuming that Val = value but I couldn't figure out what Nz meant
(I could use some humour here but I won't).

Nov 13 '05 #4
Hi and thanks for your help

I have tried entering the code into the form but found that the ClientID
was always displayed as "Alpha"01 not "Alpha"max+1 eg P01 not P08. I
changed the code to read:
Left([Client],1) & Format(Right(Nz(DMax("ClientID", "Clients", "ClientID
Like """ & Left([Client],1) & "*"""),0),2)+1, "0000")

When I put this code into the BeforeUpdate event on the form nothing is
displayed in the ClientID box and when I go to close the form or enter
another client the message "Index or Primary key can't contain a null
value" is displayed.

When I put the code into the control source for ClientID it displays the
new ID but the same message is displayed when I close etc. The same
message is also displayed when I use the original code. Any ideas on
what I am doing wrong please.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5
In your code, press F9 on the line where this runs. That will set a break
point, so you can check the values and experiment until you get the results
you want.

When the code runs and breaks, press Ctrl+G to open the Immediate window.
You can ask it for values there.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Rupert Street" <ja**********@yahoo.com.au> wrote in message
news:41**********************@news.newsgroups.ws.. .
Hi and thanks for your help

I have tried entering the code into the form but found that the ClientID
was always displayed as "Alpha"01 not "Alpha"max+1 eg P01 not P08. I
changed the code to read:
Left([Client],1) & Format(Right(Nz(DMax("ClientID", "Clients", "ClientID
Like """ & Left([Client],1) & "*"""),0),2)+1, "0000")

When I put this code into the BeforeUpdate event on the form nothing is
displayed in the ClientID box and when I go to close the form or enter
another client the message "Index or Primary key can't contain a null
value" is displayed.

When I put the code into the control source for ClientID it displays the
new ID but the same message is displayed when I close etc. The same
message is also displayed when I use the original code. Any ideas on
what I am doing wrong please.

Nov 13 '05 #6

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

Similar topics

7
by: Christopher Brandsdal | last post by:
Hi! I have a problem running my code on 2000 server and iis5.0. The code runs perfectly on my localhost (xp iis5.1) but when i run it on 2000 server iis5.0 I get this error: ...
5
by: PeteCresswell | last post by:
Access tends to take over my machine when I run long queries. The queries in question are Append queries. Some of the jobs we're talking about run 45 minutes to an hour...so the loss in...
0
by: Andy | last post by:
Hello everybody I have a question on running queries via a form. My situation is – a have a query (qrysearch) and this query has a number of fields e.g. (company, employee etc). My form is...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
4
by: deko | last post by:
I'm a little nervous about slamming my database with a dozen Update queries in a loop that all modify RecordSources of open forms. Will the use of DoEvents and/or a Sleep function ameliorate any...
1
by: jnikle | last post by:
I have a parameter query named "qry_employee_info_reports" that I need to run in the OnOpen event of a form. I'm after its total number of records. The query's got several joins in it, and one of...
25
by: artromanov | last post by:
Hello everyone. I am somewhat new to access and this forum. Hopefully it will be a great and helpful experience. My problem is this. I have a database (about 65,000 records). I get a run from...
0
by: peter palus | last post by:
Hi, I have a problem, how to cancel a running oracle-query. The situation: The user can type in a form, which kind of information he wants to now e.g. articletext like 'abc%'. This information...
3
by: Phil Stanton | last post by:
I have a number of queries which use code for the output of 1 or more fields. For example Address:GetAddress(AddressID, True, 60) Address ID Points to an Address in a table - Address Line1, Line...
7
by: Lewe22 | last post by:
I have a query pulling information from multiple tables, one of which contains a list of students with an auto number their assessment information and how long the assessment took in minutes . ...
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: 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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.