Using HAlias to have 2 active record buffers for the same file

Sometimes you need to have more than one active record buffer for a file (database table). A classic example of this is when a file relates to itself. For example, inventory records might have an alternate part to order, so there ends up a reference from one inventory record to another. We are going to take a quick look at how to accomplish this with the HAlias statement in WinDev/WebDev.

Before we jump right in and look at the code, I should mention this example is from within one of my File Manager classes. That’s right even when using the FM classes, at the end of the day Hxxxxx statements are being executed.

For this need, I could have accomplished this with a second instance of the record class and use the FM manger class to get the second copy, but for my secondary lookups inside my manage classes I tend to use Hxxxx statments, Andy tends to use FM statements. See even with wxPerts code, there is more than one right way 🙂

So on with the code2018-10-28_1347

Line 60 declares a data source variable to hold our extra record buffer, and sets its description to the same as our Inventory table, so intellisense will work for us.

Line 61 actual creates the Alias of Inventory

Line 62 is very important. In our case we want Inventory and AltInventory to refer to the same database table and that is what Line 62 is doing, making the physical name (location) the same. It is possible to define a difference location, if for example you have 2 database tables with the same physical description (say an archive or history file that isn’t defined in the analysis), but that is beyond the scope of this post.

Now you will notice that Line 65 is doing an HreadSeekFirst against AltInventory instead of Inventory, so our Inventory record buffer doesn’t get “stepped on”

Line 66 and 67 we are using the values from AltInventory.

The last thing we need to take care of is Line 76, when you declare an Alias, it lives globally until you destroy it, so a second HAlias statement trying to define AltInventory would fail. So we just cancel the definition when we are done with it.

As always with WinDev/WebDev there is more than one way to handle a requirement, but I figured this method was worth documenting since it isn’t used that often, but very handy when you run into a similar situation.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s