I’m not sure how to search for such a feature and find ideeas. I’ve been browsing for something similar for the past week now.
I want to streamline a form used to store location addresses for events. As for the database structure, the “Events” table stores a foreign key from an “Adress” table. Currently, the “Adress” table is designed with columns for “streettype”, “streetname”, “streetnumber”, “state”, “city” and “areacode”.
I can setup a form in which you can just type those in and be done with, but I wonder if it’s not better to break the “Adress” table down and create a table for each column, thus storing a bunch of foreign keys in the “Address” table.
How can I design a form for this feature? I’d like to maintain one Text Field for each address component and use a macro for keypressed to search for previously inputed and stored data (similar to the one typed in for past records) and trigger a second macro when comitting the user input, to store the ID of an exisitng entry or store that data as a new entry and then grab the new ID to store.
So in essence:
- User types some text into the Street Name textfield → macro searches for matches LIKE the one inputed and autofills
- User commits the inputed data (enter or textfield looses focus) → if matching name exists in the “streetname” table, save that entry ID as a foreign key in the current record of the “Address” table
- User commits the inputed data (enter or textfield looses focus) → if NO matching name exists in the “streetname” table, create new record there and then save that new ID as a foreign key in the current record of the “Address” table.
Any pointers regarding this? I assume something similar must exist somewhere.
I think I just found something here:
I’ll post a model for the database if I can get this to work.
Edit: I got to this point. It works how I want it to, but if you select any record and press Enter while an existing name is selected, the macro creates a new record in the address table, instead of updating the previous one.
REM ***** BASIC *****
Option Explicit
Sub KeyHandler_KeyPressed(oEvent)
If oEvent.KeyCode = com.sun.star.awt.Key.RETURN Then
CheckAndSetName(oEvent)
End If
End Sub
Sub CheckAndSetName(oEvent)
Dim sSelectedItem As String
Dim sSQL As String
Dim sSQL2 As String
Dim oForm As Object
Dim oStatement As Object
Dim vResult AS Variant
Dim vCursorTest AS Variant
REM Get text from combo box
sSelectedItem = oEvent.Source.Text
REM Get the forms
oForm = oEvent.Source.Model.Parent 'Get Form
REM Create for SQL
oStatement = oForm.ActiveConnection.createStatement() 'Create an SQL statement object
oStatement.ResultSetType = 1005
sSQL = "SELECT ""nameID"", ""name"" FROM ""name"" WHERE ""name"" = '" & sSelectedItem & "'"
sSQL2 = "INSERT INTO ""address"" (""nameFK"") VALUES (SELECT""nameID"" FROM ""name"" WHERE ""name"" = '" & sSelectedItem & "')"
oStatement.executeUpdate(sSQL2)
vResult = oStatement.executeQuery(sSQL)
vCursorTest = vResult.first
REM Check if selection already exists
If vCursorTest = "False" Then
REM Name does not exist - add to table
sSQL = "INSERT INTO ""name"" (""name"") VALUES ('" & sSelectedItem & "')"
sSQL2 = "INSERT INTO ""address"" (""nameFK"") VALUES (SELECT""nameID"" FROM ""name"" WHERE ""name"" = '" & sSelectedItem & "')"
oStatement.executeUpdate(sSQL)
oStatement.executeUpdate(sSQL2)
End If
oForm.getByName("fmtnameID").refresh()
End Sub
I tried this too, for the first sSQL2 and it returns a java error.
sSQL2 = "UPDATE ""address"" SET ""nameFK"" = ""nameID"" FROM ""name"" WHERE ""name"" = '" & sSelectedItem & "')"