How to get table's size with Access VBA? - ms-access

If Me.Subform_name.Height and Me.Subform_name.Width are the strings to get the size of the Subform "Subform_name", what are the strings to get the size of the Table "Table_name"?
EDIT:
With "Table" I mean a table of the database.

There is no such option, if you mean the table content.
If it is the measures of the table when opened and displayed (which is done internally using a form not member of the Forms collection), you can use something like:
With Screen.ActiveForm
Debug.Print .Height, .Width
End With

Related

Use AutoNumber Value as TempVariable in Access

I'm using Microsoft Access, I've got a form with three sub forms in. I want to use an auto number in the first field. Once I start entering data in the first record I'd like the auto number to set a temporary variable to use in the other sub forms. I want to set the other sub forms using the default value=[TempVars]![AUTONUMBER].
I'm only using the Macro Builder and I have a problem that the next field I want to enter data in is a required field and has a mask on it.
So Far I've tried:
>GoToControl [AUTONUMBER]
>SetTempVars
>Name: AUTONUMBER
>Expression = [AUTONUMBER]
>GoToControl [Field2]
Here my problem is that it won't let me move from field2 without having filled it in. If I take "is required" off, which I don't want to do, it allows me to go back and forth between fields, but then it screws with the masking.
Thanks in advance

"#type!" error on report calculated field

I have a report in Access with 3 fields on it: Width, Height and Area.
Width and Height are pulled from the table the report is bound to, whilst Area should be calculated (height * width). I've set the Control Source of the Area to = [Height] * [Width], but on opening the form the field displays #Type!, in typically descriptive Access errors fashion, with nice use of # and ! to make it impossible to accurately Google... but I digress. I have no idea what #Type! means and Access doesn't want to tell me.
I can't understand this. In the bound table, Height and Width are Integers, and are both populated in the record being viewed (so it's not a NULL problem). If I change the Control Source to something really simple - like =[Height], it spits out #Error! instead (again, thanks for the useful intel, Access. We'd be lost without you). Even = 1 spits out #Error!.
Any idea why Access hates my control sources?
You probably have a name clash, i.e. Access is picking up the Width and Height members of the report object rather than the fields called Width and Height.
I would create a new query and simply rename the problematic fields in it. So, if you're using the query designer:
add the primary key and any other non-problematic field, then WidthValue: Width and HeightValue: Height as additional columns;
finally, set the report's record source to be the query rather than the table directly, and update the calculated control formulae accordingly.
This is a pretty simplistic calculation. Any reason why you don't create a query, do the calculation in said query, and bind the report to the query instead? No sense killing yourself trying to figure this out.
In fact, now that I'm thinking about it, Height and Width are probably reserved words, as they're properties of a control. Maybe change them to HHeight and WWidth or something?
When access created a report it uses the field name of queries to create the name of related control on the report. then if you use the query field names later it actually refers to the report control with the same name. so solution, either rename the report field name to something different from query fields or fully qualify the query field name [query].[field] to force access to use the query field name.
Microsoft does not list Height and Width as reserved words, however, they are used when sizing forms and reports.
I had a related problem where my Form would give me this error. Changing the Form property Data Entry to Yes solved this issue. Not sure that this solves your exact issue, but perhaps in the Report Properties you can find a related field?
right click on your AREA filed and select properties then go to EVENT tab then click on "ON ENTER" select from drop down list [Event Procedure] and click on the dots on the right side and go to "MICROSOFT VISUAL BASIC FOR APPLICATION" on this window type this code above the "End Sub"
Area = Height * Width
but attention the name you enter on this code must be able on your database and as the same as you things that type in the code line
good luck

Access report field truncating at 255 characters

I have a report whose record source is a query, say qryRecords. In the report header I want to add a lengthy notes field (longer than 255 characters). I have set up a table to hold these notes (with a field type "memo"), and because that table is separate from the record source for the report, I was going to put the Notes field in the report using VBA code.
In the open event of the report, I have added this code:
Dim rst as Recordset
Dim sql_qry as String
sql_qry = "SELECT notes FROM tblNotes WHERE id = 1;"
Set rst = CurrentDb.OpenRecordset(sql_qry)
rst.MoveFirst
Me.txtNote = rst![notes] 'I get the run-time error on this line
Unfortunately I get a run-time error where noted ("You can't assign a value to this object"). I found a person with a similar issue on a form, and the suggestion was to move the code from the open event to the OnCurrent event, but that event doesn't exist for a report. Any suggestions? Thanks.
--- Original Question ---
I have a form with an unbound text box (txtNotes), and when the user opens the form, the text box is populated with text that is longer than 255 characters (it concatenates various fields from the database). This works with no problem.
There's a button on the form that opens a report, and in the report, I set the Control Source for a text box to Forms![frmMain]![frmSub]![txtNotes], which is the text box mentioned above. That works too, but for some reason the text on the report is truncated at 255 characters. The form's text box is unbound, so there's no underlying table to limit the length. Does anybody know why this would happen or if there's a workaround?
Most likely when the data from the field is being cast as a Text type rather than as a Memo. Really there's no way to explicitly cast a Text as a Memo (you can go the other way around though with CStr). I had problems with truncation on the form as well when experimenting with this scenario.
I'd suggest you are probably generating this field on the form in the wrong way. Dollars to doughnuts you could generate it in a query (used in the form's Control Source), rather than concatenating it together and placing into an unbound field. Likewise, you could then use that same query as the control source for the report that you are opening.
If all of that is truly impossible, I'd point you at dumping the values from the form into a table specifically for the report and then using the table as a control source for the report.
---- Edit ----
I still contend that you can add it to your Report's data source. In my example below, I've tacked on tblNotes to a select on the Bar table.
SELECT bar.f0, bar.f1, bar.f2, bar.f3, tblNotes.notes
FROM bar, tblNotes
WHERE tblNotes.id = 1;
Yes, if there is 300 rows of Bar, you'll get 300 copies of the same Notes field, but that's a minor problem.
Form there, you just make txtNote's data source the tblNotes.Notes column. txtNote can certainly exist in the report/page header, and when MS Access generates it, it will only use one row. I'm not sure if picks the first/last row, or random row - never the less since they are all the same, it doesn't matter.

How do I query a property on a record source for a Access 2002 report in code?

In the 'code behind' of the report is there anything I can do in code to query a property of the record source.
Sorry I'm new to access and VB, but I am wanting to achieve something along the lines of this
If Me.RecordSource["MYFieldName"] = "dan" Then
//do something
End If
Is this possible?
The only method for examining values in the recordset of a report is to have the field bound as the ControlSource of a control on the report. If it's a field that does not need to be printed on the report, then you have to add an invisible control. Deciding whether to place it in the form's header/footer or in the detail will depend on the layout of your report and what kind of data you're attempting to examine.
You used to be able to do this directly in A97 (without a hidden control), but the results were often confusing, as the data buffer behind the report is often one record ahead of what is displayed onscreen.
Also, you have to be careful which events you try to use, as the data in a report has a very different relationship to what is displayed than is the case in forms. That is, certain events cannot refer to data or controls because they happen at times where the controls do not really exist (or do not have any data in them).
In general, the only events I use in reports are OnOpen, OnNoData, OnClose and the Detail's OnFormat event, and I use them to set recordsource, controls sources, control width/visibility and to draw lines, and not much else.
EDIT: In another answer, #GuinnessFan points out something I didn't know, and that's that if you have a control on your report that is bound to the underlying field you want to refer to in code, you can do so. For instance, if you have a field called "Phone" in the underlying RecordSource and a control "txtPhone" bound to it, you can refer to Me!Phone directly in code.
My guess is that what's going on is that Access is setting up a VBA-usable wrapper only for the fields (i.e., the old hidden properties that are why you get compile-time checking of references like Me.Phone) that are used as ControlSources.
But it is still the case that to use the values from the underlying RecordSource in code, there has to be a control with that field as ControlSource, whether it's hidden or not. The new information is that you don't have to use the control name to get the value. This means it's possible to distinguish between the displayed value for a field and the value in the recordset behind the report, which are not always in alignment. I think that in general, in most situations you'll want to use the control value, as it's not clear whether the cursor of the underlying recordsource is on the same record as the one that's being displayed. Also, if you use page-level events for a report that displays more than one record on a page, it won't be obvious which record you'll be getting the data from.
If you want to do something every time the report comes across "dan"
Private Sub Detail_Format(Cancel As Integer, PrintCount As Integer)
If [MyFieldName] = "dan" Then
' do something here
End If
End Sub
For things like, "I want this field to have red text" you should look into Conditional Formating.
Used detail_format instead of print. [MyFieldName] instead of Me.MyFieldName

Referencing in forms

I have two forms in microsoft access, one called Bill and the other one called Payment. They both have Total amount as a field in both of the forms. I am trying to reference the Bill total amount to the Payment total amount.
I have tried in the Payment total amount control source : =Forms!Bill![Total Amount]
but this doesnt seem to work. In Design view it says '#Name?' in the text box.
How would you do this?
Is either of the forms a subform? If so, you need to reference the subform control or the parent property:
Me.Parent.[Total order]
Me.[Subform Control name Here].form.[Total order]
Note that the Subform Control name is not always the same as the form contained.
EDIT: Either omit Me or use Form!FormName in a control.
EDIT2: Please note that the usual way of referencing forms, subform and controls is with either bang (!) or dot (.). parentheses and quotes are rarely used. This can be seen in both the Microsoft Access MVPs site ( http://www.mvps.org/access/forms/frm0031.htm ) and Microsoft's own site ( http://support.microsoft.com/kb/209099 ), as mentioned by Knox.
If not, have you tried the Expression builder? It will help ensure that you have the correct names.
As an aside, it is best to avoid spaces in the names of fields and controls, it will save you a good deal of work. It is also best to rename controls so they do not have the same name as the fields they contain, txtOrderTotal, for example.
Remou's answer only works in code - however it looks like you are defining the control source of a text box so try this:
=Forms("Bill")![Total order]
My favorite solution is here always to go back to recordsets and calculate corresponding totals, as you are not allways sure that totals at form level are correctly updated (there might always be a pending control/recordset update for any reason). You have then the possibility to use the DSUM and related functions!
Exemple:
dsum(Forms("Bill").recordsource, "unitPrice*lineQuantity")
Of course you could have more complex solutions such as defining a temporary recordset to get your total amount per invoice.
Dim rs as DAO.recordset, _
myBillNumber as variant, _
myBillAmount as variant
set rs = currentDb.openRecordset(_
"SELECT billNumber, sum(unitPrice*lineQuantity) as totalPrice FROM " & _
Forms("Bill").recordset.name " GROUP BY billNumber")
myBillNumber = rs.fields(0)
myBillAmount = rs.fields(1)
It might seem complicated but once you get used to it you'll never hesitate to use it and you'll never ever have the kind of problem you are facing now.