Trying to create and understand a macro

Post edited for errors @16:21 12/06/21

Hi :slight_smile:

I own a small business and have been working on a database after hitting some limits with spreadsheets.

Ive made up a set of tables with firebird embedded and am in the process of trying to get the forms functional. A couple of them will need macros to work. So im hoping to start the process of figuring things out and learning macros. Final use will be for my business, single user use as we are only small. But i want to simplify the process of creating accounts etc so my wife can use it.

Im trying to create a macro that will:

  • create a new record on tbl.party
  • insert the current date into the field DateOrigination
  • set a value of 1 into the PartyTypeID field
  • copy the value of the PartyID field
  • create a new record on tbl.Person
  • Insert the PartyID value into the PartyID field

Being new to this ive decided to work with sample data and modify it. It hasn’t worked for this code. I only have a very basic understanding of sql and queries so far, hopefully that will improve over the next few weeks; though im admittedly a bit burned out from learning about table design, forms, subforms, basic sql, reports, etc etc all at once.

Im hoping someone could please take a look at the code and put me on the right tracks to get it working?

Im not sure how wrong it is. Help would be greatly appreciated, getting this working would boost morale a great deal as it would allow me to get the core of the database working as intended. If i can get this figured out, the same structure will also serve for a few other similar macros, so it will kill a few birds with one stone.

     REM  *****  BASIC  *****
    Sub AddPartyThenPerson()
       
      REM INSERT RECORDS INTO DATABASE TABLE
      Dim Context
      Dim DB
      Dim Conn
      Dim Stmt
      Dim Result
      Dim strSQL As String
      Dim PartyID as integer
      Dim PersonID as integer
    
      Context=CreateUnoService("com.sun.star.sdb.DatabaseContext") '< get  the database context
      DB=Context.getByName("FSDatabase") '<- change database name
      Conn=DB.getConnection("","") '<- username & password pair
      On Local Error GoTo CloseConn
      Stmt=Conn.createStatement()

     REM Insert current date into DateOrigination field of tblParty, and Insert PartyType 1 into Party value, and insert and return the PartyID value.
    strSQL = "INSERT INTO ""tblParty"" (""DateOrigination"") VALUES (=today());""Party"" (""PartyTypeID"") VALUES (1);Call Identity();"
   ResultSet = Statement.executeQuery(strSQL)
   If Not IsNull(ResultSet) then
      ResultSet.next
      PartyID = ResultSet.getString(1)
   End If

   REM Insert the retrieved PartyID value from tblParty into tblPerson.
  strSQL = "INSERT INTO ""tblPerson"" (""PartyID"") VALUES (" & PartyID & ");Call Identity();"
   ResultSet = Statement.executeQuery(strSQL)
   If Not IsNull(ResultSet) then
      ResultSet.next
      PersonID = ResultSet.getString(1)
   End If
   
   Stmt.executeUpdate(strSQL) '<- update the inserted data

   Conn.close("") '<- close the db connection
   
End Sub

Here is a copy of the ODB file
(it is stripped down to the tables related to the question)

Edit2:
Here is an updated file with a demo form, to give you an idea of why i need the code and how the tables work together
FSDatabase.odb

Hello,

Can see some problems with just a quick look at the code. The problem is not knowing the rest of the information - Table fields and types and some other basic set-ups. A sample of your Base file (no personal or confidential information) would be of great help. Add by editing your question. See → How do I attach a file?

That is no problem at all, ill sort a clean file now. Thank you :slight_smile:

It should be at the bottom of the post. i can upload a full copy with forms etc if required. i removed anything surplus to the question or with proprietary media on. Thanks

@tally,

Originally thought this was just a coding problem. However after a bit more digging, there are more problems. You have relations going from a primary key to another primary key. Possibly do not understand just what you are attempting. Relations are typically a link from a field to a primary key - one-to-many relation. Please clarify.

My initial thought is you are trying to do everything before anything is actually working. Macros should be the farthest point of development and in LO takes a lot of time to cover.

With more information from you about what you are doing will need some time grasp what you have here. Yes, it may be better to post the version with forms etc. Just no personal/confidential data.

its pulled from dmw consulting here:
https://www.consultdmw.com/ms-access-organisation-person-party.html

they are primary keys on person and organisation, not auto numbers. giving a 1:1 between them and the party table (which is an auto number primary key). Its done on access. on the website i copied from, so it may be wrong in libre

say you want to create a person.

A party is created first, and then you manually apply that party id to the person table. the value of partyid on tblPerson and partyID on tblParty will remain identical under the 1:1

the same process is done when creating an organisation.

Here is the thing. a party id can only be given to either one organisation or person at a time. If you try to share a partyID on an organsation and person, that would yes create a conflict.

It takes input into the tables fine in that order, but have to avoid conflicts hence want to macro it.

From that site:

"Amongst the relationships between the tables, you’ll notice two one-to-ones. The need for these is dictated by the party entity’s function, which is to link tblParty to the tables tblPerson and tblOrganisation.

tblParty supplies a unique ID to each record in tblPerson and to each record in tblOrganisation. For the model to work as intended, an ID in tblParty will appear in tblPerson or in tblOrganisation, but never in both.

Considering the record for a new organisation, for example, these are the steps to creating it:

  • Create a new record in tblParty.
  • Take the ID of this new record.
  • Create a new record in
    tblOrganisation.
  • Supply tblOrganisation.PartyID with
    the ID.
  • Complete the information in the
    organisation record.

In my databases I place a New Organisation button on the organisation form of the user interface. The on-click event of the button is coded to complete those five steps. The user is completely shielded from any activity to do with linking parties"

In short, im trying to make the code he speaks of, but with some slight differences.

I believe the benefit of taking this structure is that i can apply an address or phone number to multiple ‘purposes’ (eg home address, work site, etc) and mark them as active or inactive with dates.

I can also apply the same address/phone number to a person and company without replicating the address (by creating more ‘purposes’ for the address).

Further, i can mark a person as working for a company under the OrganisationPerson table & designate a jobrole.

This is good, as many of my clients are self employed, use the same contact details for home and work, and i sometimes use their services.

It should also allow me to preserve records for invoices, such as changes of address or name. I removed all tables relating to invoices for simplicity.

The main issue i have is this all kind of relies on code to simplify the forms. without, its a cumbersome mix of tables, input fields and master/sub forms. A draft at best, which makes little sense to anyone else. It must be filled in a set order, with various ID’s carried over. With this code (and then variations of it); i should be able to make an intuitive form.

I have added a new file, with a form for creating a ‘person’ and their details, with labels. so it may be better to understand why the code is needed and how it would be used.

Sorry but that form has an error as it is missing a table. Also very difficult for me to view as some fields are the same color as the background. Personally would not work with this.

yea thats a table i removed for the example. Missed a subform removal. Was made with dark mode theme on apologies, i was in a rush to.

Right. So the code that ratslinger gave was correct and very much appreciated. (thanks, really!). That said a step was omitted. So ive added that step in, hopefully the addition is correct. It works fine at least, does all steps i wanted and no conflicts.

The only issue is i need to manually refresh the page to see the new records. Not the end of the world though .

    Sub AddPartyThenPerson()

      REM INSERT RECORDS INTO DATABASE TABLE
      Dim Context
      Dim DB
      Dim Conn
      Dim Stmt
      Dim Result
      Dim strSQL As String
      Dim PartyID as integer
      Dim PersonID as integer

      Context=CreateUnoService("com.sun.star.sdb.DatabaseContext") '< get  the database context
      DB=Context.getByName("FSDatabase") '<- change database name
      Conn=DB.getConnection("","") '<- username & password pair
      On Local Error GoTo CloseConn
      Stmt=Conn.createStatement()

strSQL = "INSERT INTO ""tblParty"" (""DateOrigination"", ""PartyTypeID"") VALUES (CURRENT_DATE, 1)"
Stmt.executeUpdate(strSQL)
strSQL = "Select max(""PartyID"") from ""tblParty"""
ResultSet = Stmt.executeQuery(strSQL)
ResultSet.next
PersonID = ResultSet.getInt(1)
strSQL = "INSERT INTO ""tblPerson"" (""PartyID"") VALUES (" & PersonID & ")"
Stmt.executeUpdate(strSQL)

CloseConn:
   Conn.close("") '<- close the db connection
End Sub

Thanks for the patience in answering my question!

As an aside…

Also, apologies for being unable to properly explain the model i used (How to Create an Access Contacts Database). I will be reading more into why the website i followed stipulated the 1:1 connections, and whether to change my full model further before it goes into use.

Im really not sure why they are 1:1, unless to prevent an Organisation and Person sharing a PartyID. I understand it is unusual. Im getting no conflict though if i follow the order for data entry when creating a new account. The macro does seem to work reliably. Its more a why choose this method?

I put trust in the fact its from a professional consulatency firm, perhaps too blindly i do need to understand it. The author does speak of access having limitations with regards to relationships, so that type of join may not be needed in libre or some such situation. Obviously this requires further research. Thank you for the feedback. I am open to further reading should anyone have good sources for party model structures. So far i have been using vertabelo and consultdmw for reference., alongside various youtube tutorials. Cheers :slight_smile:

@tally,

You stated:

The only issue is i need to manually refresh the page to see the new records. Not the end of the world though .

Had given you the code for this in a comment about 14 hours ago! Did you click on:

image description

Also note, neither Access nor Base have relationships. Databases do. Access and Base are just front ends to databases.

found it now, cheers :slight_smile:

@ratslinger

I noticed you deleted your answers where your behaviour got all odd, then rude (spamming comments and caps yelling at me).

Your reason to delete couldn’t have been to stop me using your code since i already have it, so it’s probably to hide said behaviour.

This comment can serve as a record that it happened and that you have deleted all trace of it.

You seem to have no idea of this sites’ operation and the comment presented to you before deleting. All answer information is still in your notifications if you haven’t deleted them.

Don’t care if you use my code or not.

Don’t care about hiding anything. In comment stated:

To alleviate your concerns and possibly others will answer, I will delete my responses on the two questions.

Just trying to open the questions for others to answer. Don’t believe in the direction you want.

And as for the all caps comment, posted apology a while ago (now part of deleted answer):

Apology accepted. Sorry it all looked a bit weird at this end… Look, whether it works or doesn’t in the long term, i wanted to try that model. I didnt expect your specific help, though it was good to have it while there. I never claimed it was the best way to model, or that im sticking with it, just that i wanted to finish this build and get is useable in its intended form. I wanted it to be workable for me, sure but its too technical at 3 weeks of reading. Ultimately it was the hardest option to build out of what i found, and its allowed me to see to what extent im willing to go and what effect it could have. I can see of my own finding that its not going to be easy to manage long term, and that some basic things are in fact far harder than expected if code is to be used. I likely will use one of my more basic builds, but you dont know until you try.