I don’t have time to spoon feed this to you, but here is my working code that does this a couple of ways for one table I use. It might give you some ideas. I did have to work around the fact that LO doesn’t have the function that Access has to do this. NOTE that this is data base specific. I use MariaDB (a sister to MySQL). This all may or not help you, but if you dig in I think you’ll find how to do it.
First, I have this sub I use to move to the last record:
Private Sub DrawPage_(oForm As Object)
'mri oForm : stop
Dim oDoc As Object : oDoc = ThisComponent
oDoc.lockControllers
oForm.moveToInsertRow
oForm.Last
oDoc.unlockControllers
End Sub
Also I have a sub to move the record pointer to a specific record:
' - primitive used above (2 places) Version: MariaDB / MySQL
Private Sub FindRecord_(oEvent As Object, Text$ As String)
Dim oForm As Object :oForm = oEvent.Source.Model.Parent 'Our form - the form that holds this combo box (and has connects to the database!)
If (Not IsNull(Text$)) Then
Dim oConnection As Object :oConnection = oForm.activeConnection() 'Connect to our form's database
' Dim sLstSrc As String :SLstSrc = oForm.getByName(oEvent.Source.Model.Name).ListSource 'HSQLDB 1.8
' Dim iOrderBy As Integer:iOrderBy = InStr(sLstSrc, " ORDER BY")-1 'HSQLDB 1.8
' Dim sSelect As String :sSelect = Left(sLstSrc, iOrderBy) 'HSQLDB 1.8
' Dim sOrderBy As String :sOrderBy = Right(sLstSrc, Len(sLstSrc)-iOrderBy) 'HSQLDB 1.8 'HSQLDB
'Get SQL that created the combo box, and make it ready to nest into other SQL statements
' mri oEvent.Source.Model:stop
Dim sFROM As String :sFROM = " FROM (" & oForm.getByName(oEvent.Source.Model.Name).ListSource & ") AS Foo" 'MySQL
' Dim sFROM As String :sFROM = " FROM (" & sSelect & ") AS Foo " 'HSQLDB 1.8
'1) Get selected ID for combo box. Note, the recordset in the combo box is like this:
' 1st field: must be named "Choice" (for the WHERE below), and
' 2nd field: Must be the record's unique ID (for the getInt(2) below)
Dim sSQL As String :sSQL = "SELECT *" & sFROM & " WHERE `Choice` = ?" 'For MySQL
' Dim sSQL As String :sSQL = "SELECT *" & sFROM & " WHERE ""Choice"" = ?" & sOrderBy 'For HSQLDB 1.8
'msgbox sSQL
'msgbox Text$
Dim oSQL As Object :oSQL = oConnection.prepareStatement(sSQL) 'Create SQL prepared statement; the thing that will carry out the SQL-command
'msgbox(sSQL):stop
' oSQL.ResultSetType = com.sun.star.sdbc.ResultSetType.SCROLL_INSENSITIVE 'For HSQLDB 1.8 (not for MySQL)
Dim oResults As Object :oSQL.setString(1,Text$): oResults = oSQL.executeQuery() 'Execute SQL statement with parameter & get results
' On Error Goto ErrorHandler
' oResults.First() 'If error here then can't find the lookup string (empty dataset?), so something is wrong. Mostly ignore it.
' On Error Goto 0 'Deactivate error handling
'mri oResults:stop
'Skip if not found (because string is not yet a valid value from the list)
If oResults.First() Then
Dim iSelectedID As Integer: iSelectedID = oResults.getInt(2) 'Extract ID for the selected item (2 = column # 2)
'2) Get big list of IDs of all records. Their order needs to be the same as for the underlying table!!!
sSQL = oForm.ActiveCommand & iif(oForm.Order="",""," ORDER BY " & oForm.Order)
oResults = oConnection.createStatement().executeQuery(sSQL)
'The grid to locate the record in must be named "Grid"
' Lookdown.SetRowNumberFromID_sub(oForm, oResults, iSelectedID) 'Get a row# from the selected ID
Lookdown.SetRowNumberFromID_sub(oForm.getByName("Grid").getRowSet, oResults, iSelectedID) 'Get a row# from the selected ID
End If
End If
' Exit Sub
'ErrorHandler:
' MsgBox "Error " & Err & ": " & Error$ & " (line : " & Erl & ")" ' ... individual code for error handling
' On Error Goto 0
' Exit Sub
End Sub