Datasheet View insert value in to field when checkbox is checked - ms-access

I have a form as a Datasheet View and inside I have added a column for checkbox.
Let us say that the column with check-boxes is column A and the one from the right it is column B.
How can I insert some text in column B when the checkbox from column A is marked as checked. I need somehow to find out the current row on witch the checkbox is and take the ID for the record from the row on with checkobox is and run a SQL insert for that specific ID in the database table, something like: SQL = "UPDATE table SET columnB='string' WHERE ID= ROWid;"
How can I do the row selection part from the datasheet view?

Basically Access is not working with rows but with datasets. This means that particularly here Access is reacting very differently than Excel.
Do you have only one single checkbox checked or do you allow the user for checking different checkboxes before inserting your desired value?
Add AfterUpdate Event on Checkbox and add:
Private Sub Checkbox_AfterUpdate()
Dim strUser As String
strUser = CurrentUser()
If [Checkbox] = True Then
[UserName] = strUser
Else
[UserName] = ""
End If
End Sub
[UserName] is your (text)field in the right colum. With a user management you can use the login name. If not you could use the current user or the Windows login name.
CurrentUser() is mostly returning "Admin". If you're looking for the windows username have a look here:
Retrieve the user name from Windows

Related

Coding a button on a form in MS Access

I'm trying to build a database with MS Access. I have two tables- StockFrames and Projects, and I have a form- FrameCheckOut. On the form I have a FrameID field (where we will type in a frame id number or scan its barcode) and a ProjectName field, with a drop down of project names from the Projects table. I also have a button- Assign Frame. I want the button to update the StockFrames table with the projectID number so that I can know whether or not a frame is currently in use (or "checked out") to a project.
I have tried assigning this code to the button On Click:
UPDATE StockFrames
SET StockFrames.projectID = [SELECT Projects.projectID
FROM Projects WHERE Projects.projectName LIKE projectName]
WHERE frameID = frameID;
.. but that code contains invalid syntax. I am very new to Access and coding and I would really appreciate some help if anyone is willing.
Include key field in combobox (column can be hidden) RowSource. Value of combobox will be ID but users see and type name. Combobox properties:
RowSource: SELECT ProjectID, ProjectName FROM Projects ORDER BY ProjectName;
BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0";2"
ControlSource: leave blank if control is used to enter search criteria, otherwise field you want to save into
If form is bound to StockFrames, an UPDATE action is not needed. Find record for specific FrameID and simply select project from combobox.
If you prefer to use UPDATE, then have UNBOUND comboboxes for Frames and Projects designed as described above. Example VBA:
Private Sub AssignFrame_Click()
CurrentDb.Execute "UPDATE StockFrames SET ProjectID = " & Me.cbxProject & _
" WHERE FrameID = " & Me.cbxFrame
End Sub

Obtain Value from MS Access Checkbox

I have an Access database in which I store some data, and that database has 13 tables plus a reference one.
I want to make a form where there are several checkboxes and a button. Each chekbox represents a table, and every table selected will be joined inside a query writen in VBA, associated with the button click.
I've already made the same thing in Excel, and it works perfectly, so the only problem here is that I don't know how to access the checkbox value and use an IF condition to get the correct SQL string.
To make it clear, here I have a IF statement for one of the checkboxes in Excel:
If Range("B8").Value = True Then
CTODStrc = ", CTODTYPE, CTOD.TEMPERATURE, VALIDITY, DELTAR, DELTAL"
CTODStr = " JOIN CTOD ON REF.ID = CTOD.REF_ID"
JoinStr = JoinStr & CTODStr
Columns = Columns & CTODStrc
End If
SQLStr = RefStr & JoinStr 'Query sentence
The SQLStr is the query text, and it has a prior "select" string which is added.
In Excel, the cell B8 was associated with the checkbox, but in Access I have to make this condition using a checkbox thats in the form - how can I do it?
I've tried Me.CbName.Value, but it says the command is not supported.
Thank you.
The checked state of a checkbox is given by the Value property of the checkbox control. This property may be 0 (unchecked), -1 (checked), or Null for a block-filled triple state checkbox.
Since the Value property is the default property for a checkbox, and assuming you are not using a triple state checkbox, you should be able to use simply:
If CBName Then
' Do stuff
End If

Use a text box as a wildcard search on a field within a linked SQL Server Table within MS Access

I've managed to successfully build a combo-box that will allow a user to select a record from a list to get an ID from a table. With the table I'm currently working with running to around 60,000 records, it's not realistic to use this method to find the record.
What I want the user to be able to do is enter a name in a text box, and a combo box be populated with the relevant records from the table where one of the fields matches that. So if the user entered 'This' into the text box, the combo box would present records where the field had 'This', 'This and That' and 'this'. It would not present the record that only had 'That' in the field.
Lets say the Text box is called 'txtBox', the combo-box is called 'comBox' and the field in the linked SQL Server table 'LinkedTable' is called 'SearchField'
I would suggest you not use the combo box. Just have a text box in which the user can type in a few characters and hit enter key.
You THEN display a “list” of results that allows the user to select and click on any of the results to “edit” or “view” the given row of data.
In the following screen shot we working with a VERY small table of 500,000 rows. We looking for smith, so we just type in smi and hit enter. The results are displayed instant, and at that point the user can type in “first name” or just a few chars of first name and further drill down and filter.
The form looks like this:
The code in the after update event of the text box is simply:
Dim strSQL as String
strSQL = "select * from tblCustomers where LastName like '" & me.TextSearch & "*’"
me.RecordSource = strSQL
So very little code is required. You could I suppose fill in the results to a combo box, but then the user has to type into a box, then select something from a combo box and then somehow you bring up that record for editing. That’s like 3+ steps for the user.
Just use what Google or most accounting or darn near any computer software does:
A simple text box – you type in a few characters and when they hit enter the results are displayed for the user to pick.
Note in above the user can click on the “glasses” icon button to launch a form that displays the single record. The code behind that button is:
Docmd.Openform "frmEditDetails",,,"ID = " & ME!ID
I went down a different route to the suggested option in the other answer. This met my needs more accurately and might help someone with a similar issue.
First, I amended the query on the Combo box so that it used the value in the text box as a criteria for one of the fields.
In the Criteria for the 'SearchField' within 'LinkedTable' I entered:
Like "*" & [forms]![*FormName*]![txtBox] & "*"
This will restrict the results in comBox to those where the SearchField contains the value entered into txtBox. This doesn't update dynamically however and will only update after the first value entered into txtBox.
To get around this problem, I added the following to the 'On Get Focus' event for comBox
Private Sub comBox_GotFocus()
Dim ctlCombo As Control
' Return Control object pointing to a combo box.
Set ctlCombo = Forms!FormName!comBox
' Requery source of data for list box.
ctlCombo.Requery
End Sub
This will force the combo box to run the query again, using the current value in txtBox, each time it is 'clicked on'
As a result, I get the restricted list of values in the combo box that I need and allows the user to 'search' for a record that can then be used for creating a new record on a form.

Filter combobox on global variable

So I have this Access 2010 database into which users must login. The username they use is saved as a global variable. I then have a form which updates a table when they enter data and click a "save" button. I am trying to set one of the comboboxes (user) that is currently linked to a column in the table to be filtered on the global variable so that each person can only enter data under their own username. Is this possible? Does anyody know how to code this? I'm a complete newbie to Access and VBA and would appreciate any help
Greets
Me
In the form_load() function of that form you should fill the combobox with the global variable. To be sure they can't edit you should disable the combobox as well.
Private Sub Form_Load()
Me.myComboBoxName = gMyGlobalVariableName
Me.myComboBoxName.enabled = false
End Sub
However I'm assuming that the combobox has two columns (id and username) of which the first one is hidden and the primary key of some table where you store all the usernames. The gMyGlobalVariableName should have stored the id, not the username itself.
You can set the row source of the combo in the load event of the form to include only the relevant rows.
Me.TheCombo.RowSource = _
"SELECT UserColumn, Etc FROM TheTable WHERE UserColumn ='" _
& TheVariable & "'"
You may also wish to ensure that the form only contains the relevant records, however, the fact that you have a save button, suggests an unbound form. In Access, save buttons are largely redundant because the default is to save a record and stopping saves is the difficult bit.
I wonder why you do not use their windows log-in user name?

Allow input on an auto-number field in form to create new record in Access

I have a form that will display the last record of a table, with a few additional fields from other tables. The PK of the main table is auto-numbering. I need to be able to allow the user to make a change to the auto-numbering field, then determine if the record exists or not, if so - update, if not - insert new. I tried adding an event to the field on field change but whenever I try to click in the field on the form, it just beeps at me. Here is the code:
Private Sub JobID_Change()
'Check Bid#, if already exists open selected record for editing
rstOpenOrder.FindFirst "JobID = " & Me![JobID]
Do Until rstOpenOrder.NoMatch
With rstOpenOrder
'Add new record to end of Recordset object.
.Edit
'Edit data.
!LocationID = Me![LocationID]
!Description = Me![JobName]
!BaseBid = Me![BaseBid]
!GrossMargin = Me![GrossMargin]
!MDs = Me![ManDays]
!BidDate = Me![BidDate]
!ShortDate = Me![ShortDate]
!EmployeeID = Me![EmployeeID]
!GC = Me![GCs]
.Update
.FindNext "JobID = " & Me![JobID]
'Save changes.
End With
Loop
End Sub
If anyone can help, I would greatly appreciate it!
It's not generally a good idea to expose autonumber fields in the user interface. However, if you still want to provide the interface you've described you'll need to do the following:
Add an unbound text box (I'll call it IDLookup)
Add an AfterUpdate event to the text box to check for an existing record
If the record exists, filter the form to display that one record
If not, move the form to a new record (Me.Recordset.AddNew)
The key here is that your lookup must be done via an unbound text box. Access won't let you edit anything inside a textbox bound to an AutoNumber field (for good reason!).