Access / DCount gives a TExt field and not INT field? - ms-access

In Access 2010, I have two tables 'Contact' and 'PhoneCalls'.
I created this Query for 'Contact' as I want to see how many times I called a contact.
Query:
SELECT Contact.*, DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID]) AS Call
FROM Contact
I have build this query using the following expression:
Call: DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])
It works fine except that it creates a TEXT field instead of a NUMBER field. For instance, I need to sort this query out, but I can only sort it "A to Z" and not "smallest to largest" as it should be.
Do you have any idea on how I can solve that?

You could use CInt() to force the call count to be an integer:
SELECT Contact.*, CInt(DCount("[ID]","ColdCall"," [ColdCall]![ContactID] = " & [Contact.ID])) AS Call
FROM Contact;
Note also that using DCount() in this way is rather inefficient. If that approach works to your satisfaction then continue to use it for now, but don't be surprised if it starts to bog down as the tables grow.

Related

Expression in Textbox not passing value to the table

Obviously I am not an Access expert or I would not be asking this question. I created a small database with several tables. On a form, there are several combo boxes for the user to choose different combinations of medium, paper, sizes, etc. I have already created an expression that returns the correct value I need, but I cannot figure out how to get this value into the correct field on the table to store with the record the form is creating. Below are screen shots of the form and a couple of the tables. I have also included the expression I am using. I need the value that the expression returns to go into tbl1Artwork and populate the ArtWorkSKU field.
Expression:
=Left([PieceName],4) & [cbxArtist].Column & [cbxMedium].Column & [cbxPaperType].Column & [cbxPrintType].Column & [cbxSize].Column
The ArtWorkSKU text box is unbound as I had to type the expression in there. I am not sure if this is the correct way to accomplish the goal. In the tables below, except for the PK, all fields are Short Text.
All guidance is greatly appreciated.
Saving calculated data is usually not necessary and can be risky. Saved value can become 'out of sync' with raw data. Value can be calculated when needed.
Saving calculated data requires code (macro or VBA).
Can be as simple as Me!fieldname = Me.controlname.
Real trick is figuring out what event(s) to put code into. In your case, most likely form BeforeUpdate.
Advise not to use spaces nor punctuation/special characters (underscore only exception) in naming convention. Better would be ArtworkSKU or ArtworkSKUnum or Artwork_SKU_Num.
Create the following function in VBA:
Function UpdateSKU()
Me.ArtWorkSKU = Left(Me.[PieceName],4) & Me.[cbxArtist].Column(3) & _
Me.[cbxMedium].Column(2) & Me.[cbxPaperType].Column(2) & _
Me.[cbxPrintType].Column(2) & Me.[cbxSize].Column(2)
End Function
Then, on the form, update the After Update event property (not vba) of each "feeder" control to:
After Update: =UpdateSKU()

DSUM won't let me give it criteria

I have a few tables: Order, OrderLine, and Cost being relevant here.
In my Order entry form, I have two subforms, one for OrderLine and one for Cost. At the footer, I have a "subtotal" which I simply want to sum up my extended prices on my orderline sub form. I tried doing something like this:
=Sum([OrderLine subform].[Form]![PriceExtended])
But it gives me an #Error (without ever telling me the error anywhere, that's another annoying problem...) Well, okay that's not a problem we can just go straight to the database with it. So I tried using DSUM like so
=DSum("PriceExtended","OrderLine","OrderUnique=" & [OrderUnique])
And that gave me an error and it just made the box blink... well okay it's not picking up the OrderUnique field, so we'll try hardcoding it
=DSum("PriceExtended","OrderLine","OrderUnique=SHOP1234")
Nope, still giving me the stupid blinking... okay then, let's try no criteria
=DSum("PriceExtended","OrderLine")
And that works just fine, except of course it sums up every order ever and I only want to sum up the lines for this particular order.
So why would my DSum criteria not accept "OrderUnique=SHOP1234"?
Since OrderUnique is defined as a Text field, you need to surround the value with single quotes or double quotes. You want the criteria string to look like:
OrderUnique='SHOP1234'
, so you would hard code that as
=DSum("PriceExtended","OrderLine","OrderUnique='SHOP1234'")
or build it with
=DSum("PriceExtended","OrderLine","OrderUnique='" & [OrderUnique] & "'")

Hiding Rows with Blank Fields in Access Subreport

I have a subreport, ExpenSubrpt, in Access 2010 that’s basically a table with two columns: YearNo, and Amount. These values are taken from another table, Expenditures. Long story short, some records in my database have rows with no YearNo. What I want to do is to hide the rows that don’t have a YearNo, ie. YearNo is blank but there is something in Amount. For example:
YearNo Amount
20
1 50
I want the first row to not be visible. I’ve tried putting
"[YearNo] <> " " AND [YearNo] IS NOT NULL"
into the Data -> Filter tab, but it gives me a syntax error (missing operator). When I removed the space, the error goes away but it doesn't filter, either. I've also tried calling a query by using the OnLoad event.
Private Sub Report_Load()
DoCmd.OpenQuery "Expenditures Query"
End Sub
Query:
SELECT Expenditures.YearNo, Expenditures.Amount
FROM Expenditures
WHERE (((Expenditures.YearNo) Is Not Null));
However, this doesn't work either. I’m not really sure what to do from here, or if there’s a better way to filter this form. Any assistance would be greatly appreciated.
You might want to change your query to have the "Not" before the field name, like this...
SELECT Expenditures.YearNo, Expenditures.Amount
FROM Expenditures
WHERE ((Not(Expenditures.YearNo) Is Null));

Microsoft Access Queries Too Many Fields

I am dealing with approximately one dozen MS access tables that are all linked into a single database file.
Occasionally I would like to be able to get the union of these tables to generate a complete dataset. My attempts to do this so far result in a too many fields defined error.
Is there a way for me to do this using Microsoft access (I can use 2007 or 2010 versions)? If not, any other suggestions on how this could be achieved would be greatly appreciated.
According to the "QUERY" section of the "Access 2010 Specifications" topic in Access help, you are limited to 255 fields (columns) per recordset (query). If the "complete dataset" you want to produce will have more than 255 columns then you won't be able to do that in a single Access query.
One possibility might be to create several queries with no more than 255 columns each, export them to Excel, then paste them together side-by-side into a very wide Excel document. Excel_2010 worksheets can have up to 16,384 columns. It could be a bit tedious to do manually but there would be some opportunity to automate the process, at least in part. You'd also have to be careful that each "partial query" returned the same number of rows in the same order so things would "line up" when you pasted them together.
This answer is about a different occurrence of the "Too many fields defined" error from a query, and what I did to eliminate it.
I have a query that was giving me that error. Compacting the database didn't fix it. When I split the query into three queries with fewer fields in each, two of those smaller queries worked okay, but one still gave me the error. So it appeared that the problem had nothing to do with the number of fields, but with something about the coding of the query.
The query had two similar fields coded as subqueries:
(SELECT first([Date]) FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = SubjName and Assessment like "*Buy*") AS FirstBuyRecDate
and
(SELECT first([Date]) FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = SubjName and Assessment like "*Sell*") AS FirstSellRecDate
The error first arose when the second of those fields was added to the query. The query was able to work with either of those fields present, but not both. It was also able to work if the subquery in either of the fields was replaced with a random date constant, such as #2018 12 31#. So it appeared that the problem had something to do with those subqueries.
I was able to eliminate the error by changing those subqueries to function calls using a UDF I had previously developed for another purpose:
Public Function vFtnInQry(sField As String, sSQL As String) As Variant
' Return the value of the field with the name <sField> in the select query <sSQL>.
' The purpose of this is to allow inserting a variable value into the SQL of a query.
Dim oRecSet As DAO.Recordset, nCountRecords As Long, vRetVal As Variant
Set oRecSet = CurrentDb().OpenRecordset(sSQL)
nCountRecords = oRecSet.RecordCount
If (nCountRecords = 0) Then
vRetVal = Null
Else
vRetVal = oRecSet.Fields(sField).Value
End If
vFtnInQry = vRetVal
End Function
So the field definitions changed to:
vFtnInQry("BuyRecDate", "SELECT first([Date]) as BuyRecDate FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = """ & [SubjName] & """ and Assessment like ""*Buy*""")) AS FirstBuyRecDate
and
vFtnInQry("SellRecDate", "SELECT first([Date]) as SellRecDate FROM [C:<path>\sources.accdb].[items - subjects] WHERE [Subject name] = """ & [SubjName] & """ and Assessment like ""*Sell*""")) AS FirstSellRecDate
I don't know why this solved the problem. I'm posting it here in case someone else has a mysterious occurrence of "Too many fields defined" from a query that might be solved this way. On the other hand, if someone reading this sees a reason that those subqueries were causing that error, please post about that in a comment or another answer.

Returning specific field from a matching record

I'm still a newbie at Access 2007, but I feel I am missing a understanding of a concept, when it comes to using user input from an unbound text box.
I'm trying to have the user input the record number (i.e. A12) and return another field in the matching record (such as the record status like "Opened")
I'm fiddling with DLookup to see if it will work through that method but no luck yet.
I may look into SELECT - SQL, but I haven't used that function yet and not sure if that will give me the result I'm looking for.
If this is something elementary to access programming (or databases in general), please let me know where I can read up on this.
I am currently using the MSDN website, but examples go much further to play with.
Edit:
My DLookup so far, which happens after update from user on Text12
Me.Text14.Value = DLookup("[RecordStatus]", "Orders", Text12.Value)
Thanks
Look closer at the third option (Criteria) in your DLookup() expression. You gave it only Text12.Value, which I assume is a string value like "A12".
The Criteria parameter should be like a WHERE clause in a query, without the word WHERE. IOW, some field's value = "A12". If that field is named "record_id", try this:
DLookup("RecordStatus", "Orders", "record_id = '" & Me.Text12 & "'")