I'm using Access 2010 and I'm rusty as heck... So I create a main form and an unbound subform. Unbound to the main form, I should say, but bound to a record source. Things work fine.
In the subform, I have a dropdown called cboGIReqNbr with IDs in it. I also have a textbox called txtGIReqNbr. What's supposed to happen is that when you choose a cboGIReqNbr from the dropdown, txtGIReqNbr is supposed to populate with the description.
I've got this in the AfterUpdate event of cboGIReqNbr:
Dim db As Database
Dim rec As Recordset
Dim sSql As String
Set db = CurrentDb
sSql = "Select GI_Request_Name from tblGIRequest where GI_Request_Nbr = '" & Me.cboGIReqNbr.Text & "'"
Set rec = db.OpenRecordset(sSql)
Me!txtGIReqNbr.SetFocus
Me!txtGIReqNbr.Text = rec(0) <-- PROBLEM
Me.txtLanID = Forms!frmHoursAssigned.cboEmployee.Value
rec(0) does, in fact, populate with the correct text.
The error I get on the problem line is; "This property is read-only and can't be set". None of my objects should be read-only, and all the examples I could find online pointed to people using reserved words (i.e. using "Name" as a field name).
Anyone know how to solve this?
You should use the .Value property to assign values to a text box. .Text changes the visible value and can only be used when the field has focus. .Value stores the actual value and can be used at any time.
Me!txtGIReqNbr.Value = rec(0)
Also see: Distinction between using .text and .value in VBA Access
Related
How I can open diferents reports from one button in access if that form that shows me the information has been filtred. For example, in one form I have 2 elements, "t_element"=3 means is ID and name of report is "DNI_CAB", when "t_element"=54 means is Table and name of report is "Table_CAB". And if I filtred another form maybe shows me 8 elements. Thanks!
If I'm understanding this properly, you need to make this table-driven. By that, I mean you need to set up a table with 2 fields; ID and Report Call it tblReports.
Then you would do something like this on the OnClick event of the button:
Dim db as Database
Dim rec as Recordset
Dim MyReport as String
Set db = CurrentDB
'Find the Report you're going to use by filtering by the ID you want
Set rec = db.OpenRecordset("Select Report from tblReports where ID = " & me.txtID & "")
'Fill a variable with the resulting value
MyReport = rec(0)
'Now that we have the report name, open it
DoCmd.OpenReport MyReport
This is all entirely aircode, written off the top of my head, so it probably needs to be altered a little. But it should give you an idea of how to go about writing the code to get this done.
I am not sure as to the best way to describe this problem to make it clear to you. I have recently changed a field on a form which previously was a standard field but for the sake of user friendliness and data quality control, has now become a drop down field linked to a table and therefore provides a drop down list when the user is to enter data (instead of typing).
The issue encounterd is as follows:
This field, upon entering a value (in this case the name of a team (below listed as txtTeam) triggers an after update event so that using ADO code takes the team name, references it to an underlying team table and then copies corresponding values into the fields: "txtCity", "txtCountry", "txtCAP", "txtOfficialTeamName", ect (see below)
Unfortunately, once i changed the team field to a list field linked to the table, the If statement which I flagged below with (* HERE IS MY PROBLEM - ...) is no longer valid and unfortunately the ADO copy/paste code no longer works.
**Note: When going into the VBA editor and hovering my mouse over "Team_name" and "txtTEam" the following result appears showing me that the issue is directly linked to the fact that I have converted this field to a drop down field.
Team_name = Operational Excellence (the actual name of the team selected)
txtTEam = '71' (the ID number of the team "Operational Excellence" on the underlying table
I hope someone can help me with this because this form is really useful for me an without this code, it loses alot.
Thanks,
A
Dim rstTEAM As New ADODB.Recordset
rstTEAM.Open "tblTeams", CurrentProject.Connection, _
adOpenForwardOnly
Do Until rstTEAM.EOF
If rstTEAM!Team_name = txtTEam Then (*** HERE IS MY PROBLEM- this statement is no longer TRUE)
txtCity = rstTEAM!City
txtCountry = rstTEAM!Country
txtCAP = rstTEAM!CAP
txtOfficialTeamName = rstTEAM!Official_Team_Name
txtStreet = rstTEAM!Street
txtDivision = rstTEAM!Division
txtNumerotel.SetFocus
blnAggiunto = True
Exit Sub
Else
rstTEAM.MoveNext
End If
Loop
rstTEAM.Close
Set rstTEAM = Nothing
Now that you are using a listbox, you need to reference the control properly. I assume you are not allowing multi-select, so you should reference as follows (assuming the key is Col 0, else use 1 or 2...:
If rstTEAM!Team_name = Me.txtTEam.Column(0) Then
If allowing multi-select, you would use something like:
Dim varItem As Variant
For Each varItem In Me.txtTEam.ItemsSelected
strSQL = strSQL & Me.txtTEam.Column(0, varItem)
Next varItem
I am doing some work for someone that insists on using MS Access. I don't usually use it so I am a bit new to the whole control structure and best practices. What I am trying to achieve is to have a filter textbox on a form which, when a value is entered, it will filter the rows in the detail section. That seems like a straightforward use case. I initially tried the following behaviour as the event handler for the On Change event:
Private Sub FilterGrid()
Me.Text32.SetFocus
If Not IsNull(Me.Text32.Text) And Me.Text32.Text <> "" Then
Me.Filter = "JobNumber LIKE '*" & Me.Text32.Text & "*'"
Me.FilterOn = True
End
Else
Me.FilterOn = False
End If
End Sub
This worked perfectly until I typed something that didn't have any rows matching and the whole thing exploded with this error (and was unrecoverable without closing the form):
Run-time error '2185': You can't reference a property or method for a control unless the control has the focus.
I did some reading around and the general opinion was that .Text should not be used and .Value (or simply the Text32 without a property) should be used. That produced some very strange behaviour. The Text32.Value is ALWAYS null. I have a watch window and I can see that for the normal behaviour, Text32.Text has an actual value, but Text32.Value is NULL.
Obviously I am doing something wrong, but I don't have enough experience with Access to know what it is.
Just as an aside, another suggestion was to do Text32.SetFocus right before accessing the Text property. This doesn't resolve the error I mentioned. It still throws the exact same error.
Is anyone able to point me in the right direction here?
As you found out, the textbox's Value is only set after the control loses focus.
Conversely, the Text property is only accessible while the control has focus.
The Value property is defined as the default member for controls; that means Text32 will be implicitly the same as Text32.Value, however, depending on the context,Text32 can sometimes refer to the control itself, not just its value.
All these discrepancies can sometimes be infuriating.
To go back to the matter at hand: you have 2 ways to handle filtering.
if the list to filter is large, it's probably better that the user type their filter, then press ENTER to validate it.
if your list is not too large, you can implement filter as you type.
First case, wait for user input to be validated by ENTER.
Say your filtering textbox is called txtFilter and is located on a form whose subform is showing a datasheet (or continuous form) that you want to filter.
All you need to do is wire the textbox OnKeyDown events as such:
' We will only perform the filter if the user press the ENTER key
Private Sub txtFilter_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 13, 9
KeyCode = 0
QuickFilter
End Select
End Sub
' Perform the actual filtering on the subform
Private Sub QuickFilter()
Dim sql As String
Dim filter As String
If txtFilter.Text = vbNullString Then
' Reset the filter if the textbox is emtpy
SubForm.Form.FilterOn = False
Else
'Some common substitutions that users may have already inserted as wildchars
filter = Replace(txtFilter.Text, "%", "*")
filter = Replace("*" & filter & "*", "**", "*")
' We construct the filter SQL
sql = "([JobNumber ] LIKE """ & filter & """)"
sql = sql & " OR ([ProjectCode] LIKE """ & filter & """)"
sql = sql & " OR ([SupplierName] LIKE """ & filter & """)"
'... Add as many columns to filter on as you want
' Assign the filter to the subform
SubForm.Form.filter = sql
SubForm.Form.FilterOn = True
End If
End Sub
Second case, filter as you type
Well, it's fairly easy, we just need to add to the above solution a way to track changes as the user is typing.
This is best done through the OnChange event of the texbox.
Private Sub txtFilter_Change()
QuickFilter
End Sub
That's all you need to add.
dont use .text property
instead use .value property
text3.value=text1.value+text2.value
you dont need setfocus like .text property everytime.
it works perfectly....vba ms access
I have an unbound text box that I want to enter a number in and then it would go in a form. When I do this, I want to change the status of checkbox in another form as well. So it is like inventory. When I add this item to this box, I need the checkbox value to change because it's no longer in inventory. So the form I have is 'Info' and the checkbox is named 'ChkboxAvailableUse'. I have the checkbox correlated to a kit number and location and stuff too. So when I enter the number in an unbound text box called 'AssignKit', I want that number to look for the same kit number the 'Info' form and the 'InvKitNumber' text field, and then change that records checkbox (ChkboxAvailableUse) to change to false. I hope this makes sense. I have the line of code I thought would work below. Any help would be fantastic. Thank you
CurrentDb.Execute " UPDATE Info SET ChkboxAvailableUse = FALSE WHERE InvKitNumber = " & Me.AssignKit & ""
If you just want that form to uncheck or check in the table to match your form or subfrom then you could use a recordset.
Dim myR as Recordset
Dim strSQL as String
'This SELECT string will pull from the assignkit field in the subform
'and will be used find the matching record in the table
strSQL = "SELECT * FROM info WHERE InvKitNumber = '" & Me!subformnamehere.Form.AssignKit & "'"
'This will make your recordeset variable the one record you want to modify
Set myR = db.OpendRecordset(strSQL, dbOpenDynaset)
'Now that the recordset is pointing to that row, you can change the checkbox
'with something like this, and even use an IF statement
myR.Edit
myR![ChkboxAvailable] = True
myR.Update
'Then close the recordeset when done
Set myR = Nothing
I hope this helps, let me know if you need me to tweek it.
The sql UPDATE statement updates fields in a table. Tables don't have checkboxes, forms do, and I assume that the field (in the table) is not called ChkboxAvailableUse ?
If the field is named AvailableUse then you would update this field with:
CurrentDb.Execute "UPDATE Info SET AvailableUse = FALSE WHERE InvKitNumber = " & Me.AssignKit
Then you could ReQuery the (other) form (or a control on the form) so that it reflects these changes.
I'm pretty new to MS Access. I'm trying to create a simple form that will basically search for a particular record using a textbox, rather than a drop down box. Essentially a user would be able to enter an ID number and retrieve some other related Info. However, I do not want the user to be able to add any new records to the database. I've been able to get the forms to look the way I want them, but I'm not sure where to place the code (do I create a macro, insert the code into the properties of the button?) Any help is greatly appreciated!
I assume that you have bound your form to a table or a query and that you want to be able to enter the ID manually in a textbox, then press ENTER and load that record's data or display an error message if there is no such record.
As dsteele said, make sure that the form's Data property Allow Addtions is set to No to disallow users from adding records.
Then, from the AfterUpdate event of the textbox, add the following code (assuming that your textbox is named txtGoTo):
Private Sub txtGoTo_AfterUpdate()
If (txtGoTo & vbNullString) = vbNullString Then Exit Sub
Dim rs As DAO.RecordSet
Set rs = Me.RecordsetClone
rs.FindFirst "[ID]=" & txtGoTo
If rs.NoMatch Then
MsgBox "Sorry, no such record '" & txtGoTo & "' was found.", _
vbOKOnly + vbInformation
Else
Me.RecordSet.Bookmark = rs.Bookmark
End If
rs.Close
txtGoTo = Null
End Sub
Note that you will have to change the line rs.FindFirst "[ID]=" & txtGoTo to something that is adequate for your data:
"[ID]=" should be replaced by the field you want to search (it could be "[POReference]=" or something else.
if you are searching by a numeric ID, for instance because the field is an autonumber column, then the code is fine.
Otherwise, if the field you are searching on is a string (say PN12-G) then you have to change the code to:
rs.FindFirst "[ID]=""" & txtGoTo & """"
Failing to use the proper quoting (or quoting where not necessary) will result in errors of the kind Data type mismatch....
As a new user, I would recommend that you have a look at the sample NorthWind project database that is either shiped with older versions of Access or available as a template for download from Access 2007.
There a lots of techniques to learn from as a new Access developer, including other ways to implement record navigation.
Set the form property Data/'Allow Additions' to No.
Either in the AfterUpdate event of the textbox, or in the Click event of a button, you can write code or assign a macro to look up and display the record you want.