Using DCount function with a query linked to a control - ms-access

The following code is giving me a syntax error message. I would like to use the DCount function on the current event of a form to count records in a table where the value on the control 'TeamID' is equal to that in field 'teamID' in the table 'tblCompetency06'
Me.etcRecordNumber.Caption = "Record " & Me.CurrentRecord & " of " & DCount("ID", "tblCompetency06") WHERE [tblcompetency06].[teamID]= '" & me.Teamid & "'"

There could be two reasons, one you are using the wrong syntax for DCount. Second you are using/comparing a Number data type and you are comparing with a String. Try the following.
Me.etcRecordNumber.Caption = "Record " & Me.CurrentRecord & " of " & _
DCount("*", "tblCompetency06", "[teamID]= " & me.Teamid)

Related

updating table via SQL with variables

I'm trying to update a table, via docmd.RunSql, and can't get it to update.
idlook = DLookup("[ID]", "119_review", "[todays_date] = #" & Format("" & Me.Combo87 & " " & Me.Combo89 & " 20" & Me.Combo91 & "", Medium) & "#")
MySQL = "UPDATE 119_review SET [Earned_Income]=" & Val(EarnedIncome) & " AND [Earned_income_withcal]=" & Val(EarnedIncomeCal) & " WHERE [ID]= " & idlook & ";"
Debug.Print MySQL
DoCmd.RunSQL MySQL
I've tried it both with and without brackets on the fields, the immediate window reads:
UPDATE 119_review SET Earned_Income=62 AND Earned_income_withcal=58.4 WHERE ID= 23;
UPDATE 119_review SET [Earned_Income]=62 AND [Earned_income_withcal]=58.4 WHERE [ID]= 23;
any idea where I'm going wrong?
You've made a simple syntax error.
Different columns in an update statement should be separated by ,, not by AND.
Weirdly enough, doing this wrong doesn't throw a syntax error, but just doesn't update anything.
Change the row setting your SQL string to the following:
MySQL = "UPDATE 119_review SET [Earned_Income]=" & Val(EarnedIncome) & " , [Earned_income_withcal]=" & Val(EarnedIncomeCal) & " WHERE [ID]= " & idlook & ";"

MS Access Tag property - how its identifying update and insert operation?

I trying to create Add,update,Delete operation on MS Access form.I found this code on internet where Insert and update is happening on the same button. I am not getting what is exactly happening in below line and how it's identifying it is for update or insert.
Not getting following line : = Me.txtid.Tag & "" = ""
Please find below code which works perfect as per requirement.
'When we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtid.Tag & "" = "" Then
' this is for insert new
' add data in table
CurrentDb.Execute "insert into student(stdid,stdname,gender,phone,address)" & _
" values(" & Me.txtid & ",' " & Me.txtname & " ',' " & Me.cmbgender & " ','" & _
Me.txtphone & "', '" & Me.txtaddress & "')"
'refresh data in list on form
subform_student.Form.Requery
Else
CurrentDb.Execute "UPDATE student " & _
" set stdid = " & Me.txtid & _
", stdname = '" & Me.txtname & "' " & _
", gender = '" & Me.cmbgender & " ' " & _
", phone = ' " & Me.txtphone & " ' " & _
", address = ' " & Me.txtphone & " ' " & _
" WHERE stdid = " & Me.txtid.Tag
End If
The .Tag property is a general-purpose string property of every form and control object in VBA/VB6. It is provided as a place for developers to "put stuff" to support the operation of their applications.
The original code from which you copied your sample must have written a value to Me.txtid.Tag when the record was loaded (e.g., perhaps in the form's Current event) to indicate whether the record is an existing record or a new record (empty="new", non-empty="existing"). The line
If Me.txtid.Tag & "" = "" Then
simply checks to see if the .Tag property is empty, and then performs the INSERT or UPDATE accordingly.
BTW, re:
below code which works perfect as per requirement
No, it doesn't. Try adding a record where [stdname] is Tam O'Shanter and see for yourself. You should ditch the dynamic SQL and use one of
a bound form (as Gustav suggests),
a parameterized query, or
a recordset update.
Forget/remove all this code and bind the form to table Student to make this all happen automatically.
If a bound form is not familiar to you, browse for a tutorial for "Beginning with Microsoft Access" or the like.

DoCmd.SearchForRecord with multiple criteria in Access 2013

I have a form pulling data off an SQL Server table. The form has two unbound comboboxes. Users first select from comboName, then comboDate, then the form will retrieve the record associated with the values.
'comboDate AfterUpdate VBA macro
DoCmd.SearchForRecord , "", acFirst, "[Name] = " & "'" & comboName.Value & "'" & _
" and [Date] = " & "'" & Format(comboDate, "yyyy-mm-dd") & "'"
If I use Name = comboName.Value alone it will retrieve the first record for that name, but if I add the Date criteria, or use the Date criteria without Name, the combobox will no longer retrieve the record. It just stays on the current one. I have already converted the Access date format to match the SQL Server. What else needs to be done?
Access needs # symbols for it to recognize it as a date. It works when I change the query to this.
'comboDate AfterUpdate VBA macro
DoCmd.SearchForRecord , "", acFirst, "[Name] = " & "'" & comboName.Value & "'" & _
" and [Date] = " & "#" & Format(comboDate, "yyyy-mm-dd") & "#"

ms access error 3061 - To Few Parameters

I'm very new to coding and ms acess, so I really need your help.
I'm trying to get a button to update some data in a table.
I used this code but I keep getting this 3061 error, and i'm going crazy :( help!
Private Sub Concluido_Click() 'Add
CurrentDb.Execute "UPDATE OFP_tempos " & _
" SET Id=" & Me.txtId & _
", Tempo_total='" & Me.txtTotal & "'" & _
", Fim='" & Me.txtHora & "'" & _
" WHERE OFP_tempos=" & Me.txtId.Tag
' disable button edit
Me.Concluido.Enabled = False
End Sub
OK so I found the error at last!
(Now I feel stupid for asking -.-')
On the last line I had the name of the table OFP_tempos instead of the ID -.-'

Building a DCount/SQL statement in VBA via concetenation if test is true

I have a data entry form (Access 2007) which is designed to find out if the captured animal already has an existing WHno. Unfortunately, the data is messy and these is not a single unique identifier so several tests must be performed to narrow the search.
The animal could have 1 to 10 different pieces of information which will help identify the animal’s existence in the database. (The script only tests for about half of them thus far) I was thinking the best way to do this would to be to “build” a DCount and/or SQL statement based on which fields the user selects. I hope test to see if a particular text field box (unbound) has been filled out, and if yes, concatenate that section of code to the DCount/SQL statement, then move on to the next text field box to test.
Once the statement has been completely built, I want to test to see how many records have been counted/selected. If one record has been selected, I want to display the results in FormA. If 2 or more records are found, I want to display the records in a multi-listing form (FormB) from which the user can select the correct animal based on additional information not tested but displayed in FormB. If zero records are found, I want to create a new record with the data entered into the form updated into the table.
The hurdle I am struggling with now is building the DCount statements. I keep getting syntax errors . I do not know how to put this together piecemeal when the function bombs out because the syntax is incomplete (which it will be until I finish “building” it.)
I know the data is a mess. The scene out in the field is chaotic, different people gather different kinds of information, and not all the data that should be entered on the paper forms get filled out completely - if at all. The data gathering procedures are unlikely to change anytime soon.
Ideas? A different but easier approach idea is also welcome. New to this and not sure of all my programming options.
Also, how long can this statement be before it bombs out?
Code so far:
Private Sub GenerateWHno_Click()
Dim rs As DAO.Recordset
If IsNull(Forms!F_HotelEntry!txtSpecies) Or (Forms!F_HotelEntry!txtSpecies) = "" Then
MsgBox "Species is a required field. Please enter a species"
Exit Sub
End If
MsgBox txtSpecies
' Each line of code below indicates a data entry field(s) that needs testing and appended to SpeciesCount if "true". The first line is unchanging and is declared upfront.
'SpeciesCount = DCount("[Species]", "AnimalInfo", "(nz([Status])= '' OR [Status] = 'Alive' OR [Status] = 'Unknown') AND ([Species]= '" & txtSpecies & "')" _
' & "AND (((nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & nz(txtL_ET_No1) & "')" _
' & "AND (((nz([R_ET_Color1])= '" & Nz(txtR_ET_Color1) & "' AND nz([R_ET_No1])= '" & nz(txtR_ET_No1) & "')" _
' & "AND nz([L_ET_No2])= '" & nz(txtL_ET_No2) & "')" _
' & "AND nz([R_ET_No2])= '" & nz(txtR_ET_No2) & "')" _
' & "")
'If txtL_ET_Color Is Not Null Or txtL_ET_No Is Not Null Then
'LET1 = & "AND (((nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & nz(txtL_ET_No1) & "')" _
'Species Count = SpeciesCount & LET1
'End If
'If txtR_ET_Color Is Not Null Or txtR_ET_No Is Not Null Then
'RET1 = & "AND (((nz([R_ET_Color1])= '" & Nz(txtR_ET_Color1) & "' AND nz([R_ET_No1])= '" & nz(txtR_ET_No1) & "')" _
'Species Count = SpeciesCount & RET1
'End If
'If txtL_ET_No2 Is Not Null Then
'LET2 = AND nz([L_ET_No2])= '" & nz(txtL_ET_No2) & "')" _
'Species Count = SpeciesCount & LET2
'End If
'If txtR_ET_No2 Is Not Null Then
'RET2 = AND nz([R_ET_No2])= '" & nz(txtR_ET_No2) & "')" _
'Species Count = SpeciesCount & RET2
'End If
'There are about 4 more options/fields to add to the script but you get the idea.
'Thus: If user selected Species, and filled out L_ET_Color1 and/or L_ET_No1, the final concatenation (DCount statement)would look like this:
SpeciesCount = DCount("[Species]", "AnimalInfo", "([Status]= 'Alive' OR [Status] = 'Unknown' OR nz([Status]) = '') AND [Species]= '" & txtSpecies & "' AND (nz([L_ET_Color1])= '" & Nz(txtL_ET_Color1) & "' AND nz([L_ET_No1])= '" & Nz(txtL_ET_No1) & "')")
If SpeciesCount > 1 Then
MsgBox SpeciesCount & " Greater than 1. Please select correct animal"
'Create SQL statement that mimics DCount statement and display all fields from AnimalInfo table as multilisting to select from
ElseIf SpeciesCount = 0 Then
MsgBox "You need a new WHno"
WHno = Nz(DMax("WHno", "AnimalInfo")) + 1
MsgBox WHno
Set rs = CurrentDb.OpenRecordset("AnimalInfo")
rs.AddNew
rs!WHno = WHno
rs!Species = txtSpecies
rs!L_ET_Color1 = txtL_ET_Color1
rs!L_ET_No1 = txtL_ET_No1
rs.Update
rs.Close
Else
'Create SQL statement that mimics DCount statement and display all fields from AnimalInfo table as single listing in a form.
MsgBox "You're WHno is " & WHno & " Is this the correct WHno?"
End If
Forms!F_HotelEntry!txtSpecies = ""
Forms!F_HotelEntry!txtL_ET_Color1 = ""
Forms!F_HotelEntry!txtL_ET_No1 = ""
End Sub
I would suggest to first compose the condition into a string variable. There you can print its content via Debug.Print and see what the problem might be.
If you cannot spot the problem via inspection alone, paste the generated string to the Sql view of a proper query and see if Access gives you helpful information on switching to design view.