473,398 Members | 2,404 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,398 software developers and data experts.

VBA Function to to fill nulls/copy down values

I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.

See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....

ID BATCH
1 IMR138
2
3
4
5
6
7
8
9
10
11 7138
12
13
14
15
16
17
18
19
20
21 7141
22
23
24
25
26
27
28
29
30
31 04A49DS2007R
32

May 22 '07 #1
6 5428
Dim DB As DAO.Database
Dim Rst As DAO.Recordset
Dim NullReplace As String
Set DB = CurrentDb()
Set Rst = DB.OpenRecordset("QryIDBatch")
Do Until Rst.EOF
If Rst!Batch = Null Then
Rst!Batch = NullReplace
Else
NullReplace = Rst!Batch
End If
Rst.MoveNext
Loop
Rst.Close
Set Rst = Nothing
Set DB = Nothing

Note 1: The assumption from your post is that Batch in the first record is
not null. The code above will leave the first X records null if they are
initially null.

Note 2: The above code contains no error handling.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
re******@pcdatasheet.com


<Cl*****@gmail.comwrote in message
news:11**********************@q66g2000hsg.googlegr oups.com...
>I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.

See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....

ID BATCH
1 IMR138
2
3
4
5
6
7
8
9
10
11 7138
12
13
14
15
16
17
18
19
20
21 7141
22
23
24
25
26
27
28
29
30
31 04A49DS2007R
32


May 23 '07 #2
On May 22, 6:17 pm, "Steve" <s...@private.emailaddresswrote:
Dim DB As DAO.Database
Dim Rst As DAO.Recordset
Dim NullReplace As String
Set DB = CurrentDb()
Set Rst = DB.OpenRecordset("QryIDBatch")
Do Until Rst.EOF
If Rst!Batch = Null Then
Rst!Batch = NullReplace
Else
NullReplace = Rst!Batch
End If
Rst.MoveNext
Loop
Rst.Close
Set Rst = Nothing
Set DB = Nothing

Note 1: The assumption from your post is that Batch in the first record is
not null. The code above will leave the first X records null if they are
initially null.

Note 2: The above code contains no error handling.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
resou...@pcdatasheet.com

<Clif...@gmail.comwrote in message

news:11**********************@q66g2000hsg.googlegr oups.com...
I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.
See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....
ID BATCH
1 IMR138
2
3
4
5
6
7
8
9
10
11 7138
12
13
14
15
16
17
18
19
20
21 7141
22
23
24
25
26
27
28
29
30
31 04A49DS2007R
32- Hide quoted text -

- Show quoted text -
Thanks for you response Steve. This didnt work.

May 23 '07 #3

"Steve" <so***@private.emailaddressschreef in bericht news:iC******************@newsread2.news.pas.earth link.net...
Dim DB As DAO.Database
Dim Rst As DAO.Recordset
Dim NullReplace As String
Set DB = CurrentDb()
Set Rst = DB.OpenRecordset("QryIDBatch")
Do Until Rst.EOF
If Rst!Batch = Null Then
Rst!Batch = NullReplace
Else
NullReplace = Rst!Batch
End If
Rst.MoveNext
Loop
Rst.Close
Set Rst = Nothing
Set DB = Nothing

Note 1: The assumption from your post is that Batch in the first record is
not null. The code above will leave the first X records null if they are
initially null.

Note 2: The above code contains no error handling.

Hi Steve, back in town as we have noticed ??
It's a pity but your code is *very* bad for a (so-called) resource...

Note1: Strings can not be null
You need a line like NullReplace = Nz(Rst!Batch,"")
Note2: The line Rst!Batch = NullReplace is *never* executed
You need a line like If IsNull(rst!Batch) = true then ....
Note3: *If* the line Rst!Batch = NullReplace would be executed it would error
Of course you would need an rst.Edit and rst.Update also...

Note 4: Please do NOT advertise here !! (we have seen you...)
Same old story ?? Please, please don't !! Nobody is going to like that !!
To the OP: Code that will work:
Dim DB As DAO.Database
Dim Rst As DAO.Recordset
Dim NullReplace As String
Set DB = CurrentDb()
Set Rst = DB.OpenRecordset("TabIDBatch")
Do Until Rst.EOF
If IsNull(Rst!Batch) = True Then
Rst.Edit
Rst!Batch = NullReplace
Rst.Update
Else
NullReplace = Nz(Rst!Batch, "")
End If
Rst.MoveNext
Loop
Rst.Close
Set Rst = Nothing
Set DB = Nothing

Arno R
May 23 '07 #4
On May 22, 6:33 pm, Clif...@gmail.com wrote:
I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.

See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....

ID BATCH
1 IMR138
2
3
4
5
6
7
8
9
10
11 7138
12
13
14
15
16
17
18
19
20
21 7141
22
23
24
25
26
27
28
29
30
31 04A49DS2007R
32
I see you've requested a VBA function, but here's an alternate way to
do it using SQL:

tblBatches = the fields and data from your example

qryBatch1:
SELECT ID, (SELECT A.BATCH FROM tblBatches AS A WHERE A.ID = (SELECT
MAX(B.ID) FROM tblBatches AS B WHERE B.ID <= tblBatches.ID AND B.BATCH
IS NOT NULL)) AS NewValue
FROM tblBatches;

!qryBatch1:
ID NewValue
1 IMR138
2 IMR138
....
11 7138
12 7138
....
21 7141
22 7141
....
31 04A49DS2007R
32 04A49DS2007R

You can add that subquery to any queries you need for reports or, if
needed, update tblBatches with the values.

Note that initial Null values do what you'd expect -- they stay Null.
If qryBatch1 is changed into a Make Table query, say, tblBatchChange
then a second query can change the values in tblBatches:

qryBatch2:
UPDATE tblBatches INNER JOIN tblBatchChange ON tblBatches.ID =
tblBatchChange.ID SET tblBatches.BATCH = tblBatchChange.NewValue;

Perhaps this can be done in a single update query. Maybe I'll give
that a shot. Note that I'll exhaust the query capabilities of A97
before resorting to the capabilities added to later versions of
Access.

James A. Fortune
CD********@FortuneJames.com

May 23 '07 #5
On May 23, 1:49 pm, CDMAPos...@FortuneJames.com wrote:
On May 22, 6:33 pm, Clif...@gmail.com wrote:


I need to fill in the nulls in the batch field the value from the
record immediately
preceding the null one ie replace the nulls with the preceding value
until I hit a record with a value in it--then hold the next value
through the next set of nulls, and so on.
See example below:
I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10.
Then copy down batch "7138" all the way to ID 20 and so on....
ID BATCH
1 IMR138
2
3
4
5
6
7
8
9
10
11 7138
12
13
14
15
16
17
18
19
20
21 7141
22
23
24
25
26
27
28
29
30
31 04A49DS2007R
32

I see you've requested a VBA function, but here's an alternate way to
do it using SQL:

tblBatches = the fields and data from your example

qryBatch1:
SELECT ID, (SELECT A.BATCH FROM tblBatches AS A WHERE A.ID = (SELECT
MAX(B.ID) FROM tblBatches AS B WHERE B.ID <= tblBatches.ID AND B.BATCH
IS NOT NULL)) AS NewValue
FROM tblBatches;

!qryBatch1:
ID NewValue
1 IMR138
2 IMR138
...
11 7138
12 7138
...
21 7141
22 7141
...
31 04A49DS2007R
32 04A49DS2007R

You can add that subquery to any queries you need for reports or, if
needed, update tblBatches with the values.

Note that initial Null values do what you'd expect -- they stay Null.
If qryBatch1 is changed into a Make Table query, say, tblBatchChange
then a second query can change the values in tblBatches:

qryBatch2:
UPDATE tblBatches INNER JOIN tblBatchChange ON tblBatches.ID =
tblBatchChange.ID SET tblBatches.BATCH = tblBatchChange.NewValue;

Perhaps this can be done in a single update query. Maybe I'll give
that a shot. Note that I'll exhaust the query capabilities of A97
before resorting to the capabilities added to later versions of
Access.

James A. Fortune
CDMAPos...@FortuneJames.com- Hide quoted text -

- Show quoted text -
Thanks for your response I will try that too. Actually, this data is
imported from a fixed width text file now, i don't know if it would be
easier to write code that would read the text file line by line and
then updating the nulls in the process before importing it into a
table?

May 24 '07 #6
"Arno R" <ar****************@planet.nlwrote in message
news:46***********************@text.nova.planet.nl ...
It's a pity but your code is *very* bad for a (so-called) resource...
But his sig line is meticulously checked for accuracy before posting.

Keith.

May 24 '07 #7

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

Similar topics

1
by: RickL | last post by:
I am new to .NET (and directory services) and I need to fill a drop down list box with the users of my company. I am trying to use the code shown below, but I get this error message when the line of...
3
by: M | last post by:
I am trying to copy the values of one hashtable to another. I ran into a read only error when using the IDictionaryEnumerator. while (myHashEnumerator.MoveNext()){ while...
4
by: Tamer via DotNetMonster.com | last post by:
I'm developing a graphic engine. I got all equations made, I need only to understand how I can create a function that, once accepted three values (x; y; z), it returns the two values (X; Y) coming...
1
by: MasterChief | last post by:
I have an asp page right now and I want to fill a drop-down list with information like a persons name from an xml file. Once the user picks a person from the drop down list I want it to go back to...
0
by: --Grrrrrr-- | last post by:
Bit of a novice to this and, even worse, self-taught so could be missing something really obvious. Anyway, would very much appreciate help with: Got a SQL table with a column that has NULLS in it....
3
by: Senna | last post by:
Hi Have these to work with. int counter = 0; //The total item to fill the collection with int increment = 2; //Any number int current = 244; //Quantity int max = 1290; //Max Quantity int...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
7
by: himanshupancholi | last post by:
Hi, I need to Copy all values of a column from one table to another. Below are the details: Source: STL_GRP table, VEND column Destination PARTNER table, VEND column. I am using the below...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.