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

How to use SQL query in VBA function?

I am using Access 2010. I have a database that consists of bookings, receipts, clients and so on. I have made a form for booking tickets. The first field I have on my form is a combobox where a user can choose the username. And the second (receiptID) field should be filled automatically after the user has chosen appropriate username. I have an idea how to fill that field but what i don't have are the skills.
My Receipts table has these fields: receiptID, userID, cost, stateID, workerID
receiptID - Primary key
userID - Foreign key that points to the user this receipt is linked with
cost - shows the total cost of all bookings connected to this receipt
stateID - shows the current state of this receipt(1-in creation,2-created, 3-paid, 4-cancelled)
workerID - points to the accountant that signs the receipt

What i want to do is to fill the receiptID field on the form this way that if the selected user has any receipts in "in creation" state then the receiptID of that receipt is put there, but if there is no such receipt then new receipt is created and its receiptID is put there. I think I have to use SQL query in VBA to find the receiptID that satisfies the given conditions and if there aren't any results then new record will be created into Receipts table. I managed to create new record using VBA but at the moment every time I make a booking new record is created into Receipts table and that's not what I intended to do.
This is what I have right now for creating new record in Receipts table.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     Set dbTeater = CurrentDb
  3.     Set rsReceipts = dbTeater.OpenRecordset("Receipts")
  4.     rsReceipts.AddNew
  5.     rsReceipts("userID").Value = Me!Client
  6.     rsReceipts("stateID").Value = 1
  7.     rsReceipts.Update
  8.  
Query looks like this:
Expand|Select|Wrap|Line Numbers
  1.     SELECT Receipts.receiptID
  2.     FROM Receipts
  3.     WHERE Receipts.userID=Forms!BookingsF!ClientBox AND Receipts.stateID=1
  4.  
Apr 15 '12 #1
18 2365
nico5038
3,080 Expert 2GB
First I would like to know or there's always only one "In creation" Receipt for a customer, as it's impossible to do this "single select" in such a case.

Personally I would probably create a linked subform that will show all receipts of the selected customer ordered by ascending status and ReceiptDate (missed that field from your design, but would be usefull.
Apr 15 '12 #2
Well that's why my original idea seemed so good that there is always only one receipt in state "in creation" for each client. Because if there isn't any, new one will be created. But if there is one then this receiptID is being used until its state will be changed to something else and then it means that new receipt has to be created...
Apr 15 '12 #3
nico5038
3,080 Expert 2GB
OK, you would need the code to check for finding a record.
That can be done using the EOF (End Of File) property of the recordset like:

Expand|Select|Wrap|Line Numbers
  1.     Set dbTeater = CurrentDb
  2.     Set rsReceipts = dbTeater.OpenRecordset("Receipts")
  3.     if rsReceipts.eof then
  4.        rsReceipts.AddNew
  5.        rsReceipts("userID").Value = Me!Client
  6.        rsReceipts("stateID").Value = 1
  7.        rsReceipts.Update
  8.     endif
  9.  
So when the query returns no "1" record, a record will be added.
Apr 16 '12 #4
This code is missing the part that checks for a record that is in state "1". This far I got by myself. The real problem was how to check the properties of a record.
Apr 16 '12 #5
nico5038
3,080 Expert 2GB
I thought that receipts was the name of the query from your original post.
So just save the query e.g. named qryInCreationReceipts and use this queryname for the OpenRecordset.
Apr 16 '12 #6
Can you use eof for finding out if the recordset is empty?
Apr 16 '12 #7
nico5038
3,080 Expert 2GB
Yes, for 100% you could use a test for EOF and BOF (Begin of File), but I always use just EOF to check for an empty recordset after opening.
Apr 16 '12 #8
nico5038
3,080 Expert 2GB
To make the solution more flexible you could dynamically fill the recordset with the SQL query string and replace the Forms!BookingsF!ClientBox with concatenating the formfield: Me.ClientBox like:

Expand|Select|Wrap|Line Numbers
  1.  "SELECT Receipts.receiptID FROM Receipts WHERE Receipts.userID=" & Me.ClientBox & " AND Receipts.stateID=1"
Apr 16 '12 #9
Okay. I'll give that a try. But the query has to take one attribute from combobox. Doesn't that affect the result? Can I save that kind of query as a query in Access? I mean does Access let you save a query that includes a reference to form object.
Apr 16 '12 #10
nico5038
3,080 Expert 2GB
The value from the querybox will always be one or NULL.
You could test the value to be NOT NULL, but in general a value will be filled when you use the AfterUpdate event for your code.
Apr 16 '12 #11
Don't understand what you mean by querybox.
Apr 16 '12 #12
nico5038
3,080 Expert 2GB
Oops, should read "combobox" :-)
Apr 16 '12 #13
Well the combobox value should be from 1 to n.
Apr 16 '12 #14
My Clientbox should give values from 1 to n. So simply 0 and 1 are out of question. Sorry for double post...
Apr 16 '12 #15
nico5038
3,080 Expert 2GB
The client combobox should give a valid ClientID. When the combo isn't filled (nothing selected) the value NULL will be passed.
Apr 16 '12 #16
Ooh I understood now what you meant. Not the values are "0" and "1" but the query should give 0 values or 1 value.
Apr 16 '12 #17
nico5038
3,080 Expert 2GB
NULL and zero are different in a database environment:
0 = number indicating nothing
NULL = unknown
Apr 16 '12 #18
Finally got a decent solution. It was DLookUp function. Got everything done in 2 minutes after i found out how to use that and what was the syntax.
Apr 28 '12 #19

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

Similar topics

6
by: Brian Richmond | last post by:
I'm trying to use a regular expression to match a hidden html tag and replace it with the results of a mysql query. The query is based off a part of the hidden tag. For example: The article...
8
by: george young | last post by:
I recently found myself writing something like: def get_connection(): if tcp_conn(): if server_allows_conn(): return 'good_conn' else: return 'bad_auth' else: return 'no_server'
2
by: | last post by:
I'd want to make a custom class that will generate breadcrumb navigation for my site via a recursive query, e.g. Home > Page1 > Subpage 1. I've structured my database with hierarchical parent/child...
1
by: kevin | last post by:
I thought that the parse query feature in Query Analyzer simply wrapped your statement in a TRAN and then issued a ROLLBACK statement to prevent making real changes... but i just had a query parse...
7
by: Cyrax1033 | last post by:
I have a database that is fully operational (thanks to everyone's help) now I'm doing some housekeeping. What I'm wanting to change is one query function; I have a form that performs a search based...
4
by: Jeigh | last post by:
Been using PHP for a little while now but have never really looked into using functions and such thinking I didn't really need them (coupled with me be lazy). I've started a new project and have...
5
by: dantebothermy | last post by:
I'm looking for a function that is similar to one in excel: =max(,) I know I can write it using an iif statement, but I'm looking for an easier approach. Any ideas? Thanks, Dante
8
by: kujito | last post by:
I'm running weighted averages on ore grades for mine resource estimates. I got the wtd. avg. to come back correctly, but I only need the results out to 3 decimal places instead of the 15 it spit...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
3
by: nicolenwn | last post by:
Hello all, Many thanks in advance again for your expertise. I need help in Microsoft Access VBA once again ): I'd like to inquire if it is indeed possible to create a button that when it is...
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: 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...
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
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.