473,385 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

help me understand this code.

I am in the learning stages on MS Access and VBA for access, know more
about some parts then others. But it dones't look that bad, the VBA,
because I am used to VBA for Excel, but I am not sure about this code,
esspecially the part of what is done after the tables are selected....

Would really appreciate it :- ) if someone would kindly help me out,
thanks ahead of time.
Thank you!!!
Option Compare Database
Option Explicit

Private Sub Command0_Click()
Dim SQL As String

On Error GoTo Err_Duplicate
ADOCurrentProjectConnect

SQL = ""
SQL = "SELECT a.field5 as rectime1, b.field8 as rectime2 " & _
"FROM receiving a, dockside b " & _
"WHERE a.field2 = b.field4 AND a.field5 IS NULL;"
rst2.Open SQL, cnn2, adOpenStatic, adLockOptimistic, adCmdText
If rst2.EOF = False Then
rst2.MoveFirst
End If
Do While Not (rst2.EOF)
rst2!rectime1 = rst2!rectime2
rst2.Update
rst2.MoveNext
Loop
MsgBox "Completed. Export MS_Market and Receiving Tables as *.txt files
to Desktop."
rst2.Close
CloseConnection
Exit Sub

Err_Duplicate:
If Err.Number = errorNumDup Or Err.Number = errorNumRange Or Err.Number
= errorType Then
Resume Next
End If
Err.Raise Err.Number
End Sub

Jun 12 '06 #1
8 1510
Create an SQL Statement
SQL = ""
SQL = "SELECT a.field5 as rectime1, b.field8 as rectime2 " & _
"FROM receiving a, dockside b " & _
"WHERE a.field2 = b.field4 AND a.field5 IS NULL;"
Assign rst2 to the records returned by the SQL Statement
rst2.Open SQL, cnn2, adOpenStatic, adLockOptimistic, adCmdText
Check to see if rst2 has any records returned
If rst2.EOF = False Then
rst2.MoveFirst
End If
Loop through the records in rst2 until you get to the end
Do While Not (rst2.EOF)
Set the field rectime1 in rst2 = rectime2 in rst2 rst2!rectime1 = rst2!rectime2
Submit the change rst2.Update
Get the next record in rst2 rst2.MoveNext
Go back to Do while Statement until the last record in rst2 is
processed Loop
Message to user that the process is complete MsgBox "Completed. Export MS_Market and Receiving Tables as *.txt files
to Desktop."
Close the recordset rst2 rst2.Close
CloseConnection
Exit Sub
Code will jump here if there is an error.
Err_Duplicate:
If Err.Number = errorNumDup Or Err.Number = errorNumRange Or Err.Number
= errorType Then
Resume Next
End If
Err.Raise Err.Number
End Sub


Jun 12 '06 #2


A recordset is created based on records have field2 in one table equal to
field 4 in another table and field5 in one table empty.

the tables are receiving and dockside, aliased to a and b respectively

On every row selected into the recordset, field5 in table a is set equal to
the value in field8 in table b. The fields have aliases as well, so that
the names make some sense.

When it's all over, there is a messagebox to tell someone to export some
tables.
Jun 12 '06 #3
Thanks!!! let me think about this :- )

Ray
Rick Wannall wrote:
A recordset is created based on records have field2 in one table equal to
field 4 in another table and field5 in one table empty.

the tables are receiving and dockside, aliased to a and b respectively

On every row selected into the recordset, field5 in table a is set equal to
the value in field8 in table b. The fields have aliases as well, so that
the names make some sense.

When it's all over, there is a messagebox to tell someone to export some
tables.


Jun 12 '06 #4
RompStar wrote:
I am in the learning stages on MS Access and VBA for access, know more
about some parts then others. But it dones't look that bad, the VBA,
because I am used to VBA for Excel, but I am not sure about this code,
esspecially the part of what is done after the tables are selected....

Would really appreciate it :- ) if someone would kindly help me out,
thanks ahead of time.
Thank you!!!
Option Compare Database
Option Explicit

Private Sub Command0_Click()
Dim SQL As String

On Error GoTo Err_Duplicate
ADOCurrentProjectConnect

SQL = ""
SQL = "SELECT a.field5 as rectime1, b.field8 as rectime2 " & _
"FROM receiving a, dockside b " & _
"WHERE a.field2 = b.field4 AND a.field5 IS NULL;"
rst2.Open SQL, cnn2, adOpenStatic, adLockOptimistic, adCmdText
If rst2.EOF = False Then
rst2.MoveFirst
End If
Do While Not (rst2.EOF)
rst2!rectime1 = rst2!rectime2
rst2.Update
rst2.MoveNext
Loop
MsgBox "Completed. Export MS_Market and Receiving Tables as *.txt files
to Desktop."
rst2.Close
CloseConnection
Exit Sub

Err_Duplicate:
If Err.Number = errorNumDup Or Err.Number = errorNumRange Or Err.Number
= errorType Then
Resume Next
End If
Err.Raise Err.Number
End Sub


There is some room for improvement here. It would appear that a single SQL Update
statement might suffice instead of utilizing a recordset and 10-20 lines of code. Faster
too. Something like:
sSQL = "<copy your working update query SQL into this string>"
CurrentProject.Connection.Execute sSQL, , adExecuteNoRecords

There is no need to initialize the SQL variable to an empty string. (SQL = "")

rst2 has a wider scope than necessary. It should probably be defined within the proc.

The error handler should close the recordset before raising an error. (Err.Raise ...)

There may be some unknown (to me) reason the coder did it this way but I can't imagine
why. So when analyzing the proc it is OK to question whether the original coder did a good
or bad job...

--
'---------------
'John Mishefske
'---------------
Jun 13 '06 #5
Yes you are right, I am just trying to understand the code, I got a
little hunged up on that SQL statement, but now I understand it, kinda
weird :- )

Am I thinking right for the SQL part ?

SQL = ""
SQL = "SELECT a.field5 as rectime1, b.field8 as rectime2 " & _
"FROM receiving a, dockside b " & _
"WHERE a.field2 = b.field4 AND a.field5 IS NULL;"

select field5 from receiving table and alias as rectime1
select field8 from dockside table and alsias as rectime2

then using the WHERE clause,

for every record WHERE field 2 (receivng table ) = fiend4 in (dockside
table)
AND
field 5 in (receiving table) = Null

If that WHERE condition is True, then grab every record row for each
field in the SELECT statement and return it. Am I thinking right ? if
not please correct using simple language, I am still learning.

Thank you.
John Mishefske wrote:
RompStar wrote:
I am in the learning stages on MS Access and VBA for access, know more
about some parts then others. But it dones't look that bad, the VBA,
because I am used to VBA for Excel, but I am not sure about this code,
esspecially the part of what is done after the tables are selected....

Would really appreciate it :- ) if someone would kindly help me out,
thanks ahead of time.
Thank you!!!
Option Compare Database
Option Explicit

Private Sub Command0_Click()
Dim SQL As String

On Error GoTo Err_Duplicate
ADOCurrentProjectConnect

SQL = ""
SQL = "SELECT a.field5 as rectime1, b.field8 as rectime2 " & _
"FROM receiving a, dockside b " & _
"WHERE a.field2 = b.field4 AND a.field5 IS NULL;"
rst2.Open SQL, cnn2, adOpenStatic, adLockOptimistic, adCmdText
If rst2.EOF = False Then
rst2.MoveFirst
End If
Do While Not (rst2.EOF)
rst2!rectime1 = rst2!rectime2
rst2.Update
rst2.MoveNext
Loop
MsgBox "Completed. Export MS_Market and Receiving Tables as *.txt files
to Desktop."
rst2.Close
CloseConnection
Exit Sub

Err_Duplicate:
If Err.Number = errorNumDup Or Err.Number = errorNumRange Or Err.Number
= errorType Then
Resume Next
End If
Err.Raise Err.Number
End Sub


There is some room for improvement here. It would appear that a single SQL Update
statement might suffice instead of utilizing a recordset and 10-20 lines of code. Faster
too. Something like:
sSQL = "<copy your working update query SQL into this string>"
CurrentProject.Connection.Execute sSQL, , adExecuteNoRecords

There is no need to initialize the SQL variable to an empty string. (SQL = "")

rst2 has a wider scope than necessary. It should probably be defined within the proc.

The error handler should close the recordset before raising an error. (Err.Raise ...)

There may be some unknown (to me) reason the coder did it this way but I can't imagine
why. So when analyzing the proc it is OK to question whether the original coder did a good
or bad job...

--
'---------------
'John Mishefske
'---------------


Jun 13 '06 #6
That's it.
Jun 13 '06 #7
Ok, now this stuff is starting to make sense, lol..

Thanks!!!
Rick Wannall wrote:
That's it.


Jun 13 '06 #8
"RompStar" <rm*******@comcast.net> wrote
Ok, now this stuff is starting to make sense, lol..

When it really seems to make sense is the time to run away, screaming, into
the night. <GRIN>
Jun 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: HLong | last post by:
I am trying to understand an example where the Least Significant Bit is replaced. The code is in C# and I am having problems with a line that reads: B= (byte) (Value == 1 ? B | (1 << poss) : B & ~...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
3
by: DM | last post by:
newbie trying to set up a Database and keep getting the same result. ERROR An error occured while retrieving the information from the database: Unable to cast COM object of type...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
7
by: Mickyd1561 | last post by:
Hey everyone I'm new to this groups thing and thought maybe someone can help me. My problem is that I can't view specific images on only one website. www.baseballu.net is my baseball team's...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.