I need the code to execute the following logic:
- get a record with the maximum value of the "Sort" field;
- get the value of the "Student IDS" field of the found record;
I made the code, but it does not work.
In the line valueSort =! [NameFieldSort]
I get an error:
Run-time error '3265': Element not found in this family.
Code. Form "frm_03_03_Students_Tape". Button "AddRecor_btn" (Add).
Private Sub AddRecor_btn_Click()
Dim nameTable As String ' Имя Таблицы
Dim nameFieldID As String ' Имя поля - `ID`
Dim nameFieldSort As String ' Имя поля - `Сортировки`
nameTable = "тбл_02_Студенты" ' Имя Таблицы
nameFieldID = "ИДСтудента" ' Имя поля - `ID`
nameFieldSort = "Сортировка" ' Имя поля - `Сортировки`
Call AddRecord_md.AddRecord(nameTable, nameFieldID, nameFieldSort)
End Sub
Code. Module "AddRecord_md".
Public Sub AddRecord(nameTable As String, nameFieldID As String, nameFieldSort As String)
Dim rst As Recordset
Dim valueSort As Integer
Dim strSQL As String
strSQL = "select [" & nameFieldID & "], [" & nameFieldSort & "] " & _
" from [" & nameTable & "]" & _
" WHERE [" & nameFieldSort & "] = (" & _
" select max([" & nameFieldSort & "]) " & _
" from [" & nameTable & "])"
Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset) '
With rst
' valueSort = !Fields(nameFieldSort)
valueSort = ![nameFieldSort]
End With
End Sub
Question.
How to choose a record and all the values of its fields by the maximum value of one of the fields?
Update - 1.
1. The table "tbl_02_Students" contains records - 12 pieces.
2. I am debugging.
During debugging in the strSQL
variable, I get a query:
select [Student ID], [Sort]
from [tbl_02_Students]
WHERE [Sort] = (select max ([Sort]) from [tbl_02_Students])
I create a request in Access.
select [Student ID], [Sort]
from [tbl_02_Students]
WHERE [Sort] = (select max ([Sort]) from [tbl_02_Students])
I ask Access fulfill the request.
I get an entry in which the field Sort`` =
9, and should be
12`
3. The line of code valueSort =! Fields (nameFieldSort)
throws an error
Run-time error '3265': Element not found in this family.
Update - 2.
1. I solved the problem "Update - 1.". p.1, p.2.
Table "tbl_02_Students". Field "Sort".
It was a text;
It has become a number.
The request is working correctly.
2. "Update - 1.". p.3.
Changed the line.
It became - valueSort =!Сортировка
.
Everything works correctly.
Question.
1. How to use the syntax correctly, so that it can be in the string valueSort =!Сортировка
. pass the field name as a variable?