SQL vs MACROS

GlennG

Board Regular
Joined
May 20, 2002
Messages
80
Hello all,

I'm new to Access and I want to get user opiniions about which is the best method to manipulate data in Access. Is SQL a better alternative or are macros ? I love VBA for Excell ; can I find bliss with Access ? Or do I have to start over with SQL ?

Thanks for your comments... :confused:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Both have there own part to play and you will find that you will end up with both.

VBA is fine for functions and manipulating the appearence of theings but when you want to work on whole sets of data then SQL is much faster.

The easiest way to see how SQL works is to create queries that do more or less what you want, then go into the SQL view of the query and copy the SQL from there and modify it to suit your needs for VBA.



Shout if you need help with any particular item.

Peter
 
Upvote 0
A little terminology

SQL mainly refers to a mostly universal language syntax to work with databases. To the best of my knowledge, all versions are similar, if not identical in syntax structure. Once you learn one, you have a major headstart learning others. Unless you're working interactively only, most of the time you end up including SQL within what you're calling 'macros' to achieve a desired result.

Macros, in my opinion, is a dated term and 'old functionality'. The actual implementation within Access97+ is really a partial-step towards VBA programming. Although you can specify a series of actions to be performed, you lack the ability to handle errors, or handle branching logic.

Visual Basic for Applications programming on the other hand, probably shouldn't be termed 'Macros' - you create Functions and Subroutines that are saved in within Modules that collectively form a Procedure for performing whatever task is needed. Interestingly, at least within Access, many of the VBA Methods are really Macros (DoCmd.?????). The latter is why sometimes what you call things might be viewed as a grey area. Why calling it a 'Macro' isn't 100% incorrect.

Repeating Bat17 - in most cases, running a SQL query is probably going to run alot faster than doing it from code. A common VBA technique is to 'walk the recordset'. Basically, this takes the form of:

Code:
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Set dbs = CurrentDB()

Set rs = dbs.OpenRecordset(sqlCommand, dbOpenSnapshot)

With rs
  Do Until rs.EOF
    ' Code to look at the fields within each given record.

    .Movenext
  Loop
End With

The above uses DAO (Data Access Objects) to define variables to open the database, open a recordset, identify what is in the recordset, and then loop through it within a Do...Loop until it finds the End of File (rs.EOF) .MoveNext is really the command "rs.MoveNext" but the 'With..End With' structure allows you to avoid typing some of the information out.

This lets you refer to fields within each record as:

Code:
.Fields(fieldname).Value
!fieldname
.Fields(a_column_number).Value

vs


Code:
rs.Fields(fieldname).Value
rs!fieldname
rs.Fields(a_column_number).Value

Also repeating Bat17, depending on what you need to do, you may end up updating individual fields/records using recordset objects, or, particularly, when there are a lot of records to update, you use SQL instead.

Code:
strSQL = "Update tblname Set fieldname1 = value1 "
strSQL = strSQL & "WHERE indexfield = value2"

The above demonstrates syntax to update fieldnam1 in tablename where an index field's value is something. The WHERE Parameter is optional. Removing it would update ALL records in the table regardless of whether there is only 1 or 50,000.

The latter is why SQL is so powerful and easy to use. Where else could you use a total of 5 words to update an unlimited number of records?

The "walking the recordset" option above required 9 lines and I didn't even include the the ones that would do the field update. A VBA using SQL option would only require none of the above - one line for the Query, one for the command to execute it.

I realize I just wrote a novel for what may be a request for a quick answer, but lets just say I'm in a talkative mood today.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,657
Messages
6,161,084
Members
451,684
Latest member
smllchng5

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top