1
HI
i have database with many tables. In the first table i have a field called status.
[PHP]table 1
idno name status
111 hjghf yes
225 hjgjj no
345 hgj yes
other tables could have same idno with different fields.
2nd table
idno salary expense
111 456 469
225 42
345 456[/PHP]
I want to check the status for each id no and if it is yes then for that id number in all tables for all null and blank fields i want to update them as 111111.
I want the the field salary for id no 345 for which status is yes in first table to be updated by 111111. like this i need a loop as there are many tables in the database
I am looking for a sample vba code for this which i can adapt.
Thanks
74 4590 NeoPa 32,556
Expert Mod 16PB
Start by using a For Each loop for all the TableDefs in CurrentDB.
Within that, exclude any you're not interested in.
Within the loop set up a RecordSet (rs) with SQL incorporating each table linked to [Table1] on the ID field.
Process through the recordset ensuring all empty fields are set to "111111" (or 111111). Use rs.Edit & rs.Update around any changes to allow them to be applied.
Hi I v started like this -
Sub UpdateNulls()
-
Dim strSQL As String
-
Dim rs As DAO.Recordset
-
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "Msys" And tdf.Name <> "table1" Then
-
strSQL = "Select * From [" & tdf.Name & "] a Inner Join " _
-
& "table1 On a.fall = table1.fall Where a.Status = 5"
-
-
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
-
Do While Not rs.EOF
-
For i = 0 To rs.Fields.Count - 1
-
If IsNull(rs.Fields(i)) Then
-
rs.Edit
-
rs.Fields(i) = 111111
-
rs.Update
-
End If
-
Next
-
rs.MoveNext
-
Loop
-
-
End If
-
Next
-
End Sub
but i am getting runtime error 3075 at a.fall = table1.fall
in the strsql statement.
can anyone help me out wit teh reason
NeoPa 32,556
Expert Mod 16PB
I will start looking at this for you, but it would be very helpful to see the error message too.
For clarity - the error occurs at lines #6/7 in your code.
NeoPa 32,556
Expert Mod 16PB
I've checked through those lines and I cannot see anything which might throw an error.
Can you confirm the line number of the line the error is actually occurring on, as well as the error message itself, to give us something more to go on.
PS. You've clearly gone a long way with the concept with just the outline of some clues. Nice going :)
i have made te code work
what i am trying to do now is instaed of updating all field i am trying to update only certain fields.
this list of certain fields, i am planningto give througha text file. -
ub UpdateNulls()
-
Dim strSQL As String
-
Dim rs As DAO.Recordset
-
-
-
-
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "Msys" And tdf.Name <> "01UMWELT" Then
-
strSQL = "Select * From [" & tdf.Name & "] t Inner Join 01UMWELT On t.fall = [01UMWELT].fall Where [01UMWELT].Status = 6"
-
-
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
-
-
-
Dim variable
-
-
Open "C:\Documents and Settings\TAYYAPP\Desktop\testfile.txt" For Input As #1
-
-
-
-
Do While Not rs.EOF
-
-
Do While Not EOF(1)
-
For i = 0 To rs.Fields.Count - 1
-
-
Line Input #1, variable
-
Debug.Print TextLine
-
If IsNull(rs.Fields(variable)) Then
-
rs.Edit
-
Debug.Print rs.Fields(variable)
-
rs.Fields(variable) = 888
-
rs.Update
-
-
-
Close #1
-
End If
-
Next
-
rs.MoveNext
-
Loop
-
Loop
-
-
End If
-
Next
-
End Sub
-
-
but i am getting an error runtime 62 input past end of line at Line Input #1, variable
i think i am confusing up with teh text file calling ?
thanks
atks
i have cleared that runtime error
but now i am getting a runtime erro 3265
at -
If IsNull(rs.Fields(variable)) Then
-
what could be wrong
NeoPa 32,556
Expert Mod 16PB
I assume this is at line #29 of the code in post #6?
I also still need an error message please. I can't see anything obviously wrong, so the error message is necessary (always a good idea to provide it anyway) to give me a clue.
yes it is in line 29
the error is element in listing cannot be found.
ps: when i am checking whether the fields of that variable to be null i am meaning it column wise is this correct
NeoPa 32,556
Expert Mod 16PB
Right, it looks like line #28 has been left over from a previous version.
What you need to do is to change things so that they display (Debug.Print or MsgBox() are both ok for this) the line just read in from the file. I suggest you actually scrap the variable called "variable" as that is a reserved word and shouldn't ever be used as the name for a variable (It's also very likely to confuse - who needs that).
When you can see what the variable contains, you will see that it probably doesn't match the name of any of your fields. That is your problem I'm fairly sure.
well i changed the 'variable'
but yes the error still occurs and the varaiable reflects a field in the immediate window which is there in the field
NeoPa 32,556
Expert Mod 16PB
Can you post exactly (I'll say that again for emphasis - EXACTLY) what the value displayed is, and the names of all the fields in the recordset please.
well the field i am trying to update is ortsl which is taken from the text file
I am not sure which value displayed you are asking for.
error is 3265 "element in the listing canot be found"
Is there some way i can view the contents of the record set like some debug statement to display the recordset
NeoPa 32,556
Expert Mod 16PB
Also, could you please repost the current live version of UpdateNulls().
here is the current version -
Dim rs As DAO.Recordset
-
-
-
-
-
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "Msys" And tdf.Name <> "01UMWELT" Then
-
strSQL = "Select t.* From [" & tdf.Name & "] t Inner Join 01UMWELT On t.fall = [01UMWELT].fall Where [01UMWELT].Status = 6"
-
-
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
-
-
-
Dim varii As String
-
-
Open "C:\Documents and Settings\TAYYAPP\Desktop\testfile.txt" For Input As #1
-
-
-
-
Do While Not rs.EOF
-
-
Do While Not EOF(1)
-
For i = 0 To rs.Fields.Count - 1
-
-
Line Input #1, varii
-
-
Debug.Print rs.Fields(i)
-
Debug.Print varii
-
-
-
If IsNull(rs.Fields(varii)) Then
-
-
-
rs.Edit
-
Debug.Print rs.Fields(varii)
-
rs.Fields(varii) = 888
-
rs.Update
-
-
-
Close #1
-
End If
-
Next
-
rs.MoveNext
-
variable_ok:
-
Loop
-
Loop
-
-
End If
-
Next
-
End Sub
-
NeoPa 32,556
Expert Mod 16PB
well the field i am trying to update is ortsl which is taken from the text file
I am not sure which value displayed you are asking for.
I am looking for the value sent to the Immediate pane by your line #28 (Debug.Print...).
Is there some way i can view the contents of the record set like some debug statement to display the recordset
Try the following code entered into the Immediate pane : - For Each fld in rs.Fields : ?fld : Next fld
Be sure to run it only when the code is at a point where rs is set up.
NeoPa 32,556
Expert Mod 16PB
Try the following code entered into the Immediate pane : - For Each fld in rs.Fields : ?fld : Next fld
Be sure to run it only when the code is at a point where rs is set up.
Unless you're after the names of the fields instead, in which case use the following instead : - For Each fld in rs.Fields : ?fld.Name : Next fld
NeoPa 32,556
Expert Mod 16PB
Try replacing lines #28 & #29 with the following and posting what it says when the code fails : - Debug.Print "rs.Fields(i) = '" & rs.Fields(i).Name & "'" & vbCrLf & _
-
"varii = '" & varii & "'"
Remember to use Copy / Paste to get the info into the post for me if you would.
Try replacing lines #28 & #29 with the following and posting what it says when the code fails : - Debug.Print "rs.Fields(i) = '" & rs.Fields(i).Name & "'" & vbCrLf & _
-
"varii = '" & varii & "'"
Remember to use Copy / Paste to get the info into the post for me if you would.
rs.Fields(i) = 'FALL'
varii = 'ORTSL'
this is teh result in immediate window
fall is the first field of the table
Unless you're after the names of the fields instead, in which case use the following instead : - For Each fld in rs.Fields : ?fld.Name : Next fld
regarding this i am actually a beginner in vba so i am not sure what you are asking me to do.
should i enter this code in immediate window
then how do i run it from immediate window
NeoPa 32,556
Expert Mod 16PB
rs.Fields(i) = 'FALL'
varii = 'ORTSL'
this is teh result in immediate window
fall is the first field of the table
Clearly these do not match. Maybe because they come in a different order (Doesn't seem promising).
Let's look at the full field list to see what we have as choices. I will post a new version of your code to test this out with.
NeoPa 32,556
Expert Mod 16PB
Before I continue, what are you trying to do with the data found in the text file?
I thought it was to tell you the name of the field but it doesn't seem to be used for that in the code.
yes it is supposed to be the name of the field which i am trying to update
NeoPa 32,556
Expert Mod 16PB
So, why do you need the name of the field supplied separately (from an outside source)?
You already have the field itself (rs.Fields(i)) as well as its name (rs.Fields(i).Name). I'm a little confused.
I dont want to update all fields i want to update only those fields which are provided in the text file.
like this i can add or remove the fields which have to be updated from time to time.
NeoPa 32,556
Expert Mod 16PB
Ah. In that case the logic of your code is fairly wrong.
Give me a while & I'll post something for you that incorporates the logic you're after.
NeoPa 32,556
Expert Mod 16PB
Try this : - Private Sub UpdateNulls()
-
Dim rs As DAO.Recordset
-
Dim varii As String, strField As String
-
Dim astrFields As Variant 'Stores the array of Field names
-
Dim intIx As Integer
-
-
Open "C:\Documents and Settings\TAYYAPP\Desktop\testfile.txt" For Input As #1
-
varii = ""
-
Do While Not EOF(1)
-
Line Input #1, strField
-
varii = varii & "," & strField
-
Loop
-
Close #1
-
astrFields = Split(varii, ",") 'Element 0 empty
-
-
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "Msys" And tdf.Name <> "01UMWELT" Then
-
strSQL = "SELECT t.* " & _
-
"FROM [" & tdf.Name & "] AS t INNER JOIN " & _
-
"01UMWELT " & _
-
"ON t.fall=[01UMWELT].fall " & _
-
"WHERE [01UMWELT].Status=6"
-
-
Set rs = CurrentDb.OpenRecordset(strSQL)
-
-
Do While Not rs.EOF
-
For intIx = 1 To UBound(astrFields)
-
With rs
-
On Error Resume Next 'Ignore field if table doesn't have it
-
Call Err.Clear
-
varii = Nz(.Fields(astrFields(intIx)), "NullValue")
-
If Err.Number <> 3265 Then
-
If varii = "NullValue" Then
-
Call .Edit
-
.Fields(astrFields(intIx)) = 888
-
Call .Update
-
End If
-
End If
-
On Error GoTo 0 'End of special handling
-
End With
-
Next intIx
-
rs.MoveNext
-
Loop
-
End If
-
Next
-
End Sub
thanks i had a feeling there was something wrong when i was looping within the loops
NeoPa 32,556
Expert Mod 16PB
Please check again. I posted at pretty well the same time as you did.
Thanks very much .
i just checked the code.
i have come across two things
firstly the first table is not getting updated
secondly after all the tables are checked the loop is not ending i am gettin
run time error 3061 one parameter expected but too few parameters
i put a watch on the table definition and I see that after the last table
the value of tabledef in watch window is " MsysAccessobjects"
NeoPa 32,556
Expert Mod 16PB
Well, let's start by adding some code in where line #15 is currently. This is simply for debugging so doesn't need to stay in long-term. - For intIx = 1 To UBound(astrFields)
-
Debug.Print "'" & astrFields(intIx) & "'",
-
Next intIx
Can you post me back the results please.
BTW Debugging remotely is not very reliable so great care needs to be taken. I am taking this a step at a time.
these are the results
'ORTSL' 'hursau' 'folge1' 'arztanh' 'alter2' 'kopfumf' 'gcs2' 'babin' 'vteil' 'kemler22' 'hklasse' 'kopf20' 'tueravl' 'ORTSL' 'hursau' 'folge1' 'arztanh' 'alter2' 'kopfumf' 'gcs2' 'babin' 'vteil' 'kemler22' 'hklasse' 'kopf20' 'tueravl'
these are the field i am giving as input in the text file
NeoPa 32,556
Expert Mod 16PB
Can you explain why each of the fields is included twice?
oh sorry about that my mistake
I had run the code twice
NeoPa 32,556
Expert Mod 16PB
OK. That's good :)
Now, let's replace that code with some data about the tables and post that information for me if you will. - For Each tdf In CurrentDb.TableDefs
-
Debug.Print "'" & tdf.Name & "'",
-
Next tdf
BTW, you should always use Option Explicit in all of your modules. That way the easier to find and fix errors will be found for you. Syntax etc.
With that set you will also need to add in the following line that I omitted from the original code (after line #2 I suggest) :
Here s the result
'01UMWELT' '03_PERSDAT' '04_VERLUEB' '05_VERL' '06_RETTUNG' '07_CRASHPER' '08_PKW' '10_LKW' '11_LKWB' '12_ZWEIRAD' '13_INNEN' '14_HAENGER' '15_FZGA' '16_FZGI' '17_BEFRAG' '18_FGDAT' '19_ZWDAT' '20_REKO' '22_SEQUENZ' '23_MPERS' '24_SITZE' '25_FZGAS' '28_VUANZMF' '29_VUANZBET' '30_VUANZ' '50_FZG1' '51_FZG2' '52_PKWB1' '53_PKWB2' 'MSysAccessObjects' 'MSysAccessXML' 'MSysACEs' 'MSysObjects' 'MSysQueries' 'MSysRelationships'
i see that system tables are also getting included when they should nt be.
NeoPa 32,556
Expert Mod 16PB
Actually, in this list, they should. This is all good.
i was trying step by step trouble shooting and at this step -
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "Msys" And tdf.Name <> "01UMWELT" Then
-
strsql = "SELECT t.* " & _
-
"FROM [" & tdf.Name & "]AS t INNER JOIN " & _
-
"[01UMWELT] " & _
-
"ON t.fall=[01UMWELT].fall " & _
-
"WHERE [01UMWELT].Status= 6"
even though tabledef is "msys" the control is goin to strsql
I have cleared the run time error i am not sure if this is the answer but in the code
sql "Msys" i changed it to "MSys"
and now the runtime error is gone( i hope for good)
so now have to check out updation of first table
also for the first table not getting updated i figure that the sql query if changed to the following -
strsql = "SELECT * " & _
-
"FROM [" & tdf.Name & "] t INNER JOIN " & _
-
"[01UMWELT] " & _
-
"ON t.fall=[01UMWELT].fall " & _
-
"WHERE [01UMWELT].Status= 6"
-
the first field gets updated
but if change it in the above method i get an runtime error 3190 too many fields defined
NeoPa 32,556
Expert Mod 16PB
...
so now have to check out updation of first table
As the first table is '01UMWELT' and you said you didn't want to update '01UMWELT', what is the problem?
NeoPa 32,556
Expert Mod 16PB
...
sql "Msys" i changed it to "MSys"
That is the change that is necessary. The string comparison here is case-sensitive.
actually i do want the first table also to be updated so i changed the sql to followeing -
-
For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "MSys" Then
-
strsql = "Select t.* From [" & tdf.Name & "] t Inner Join 01_UMWELT On t.fall = [01_UMWELT].fall Where [01_UMWELT].Status = 4"
-
-
but still the first table is not gettin updated. i tried trouble shooting and can see that the code is running through the first table and for ceratin paramerters even though null values exist varii is returning some number and for some fileds even though varii is returning null values update is not taking place but as the loop progresses to next table the code works fine
NeoPa 32,556
Expert Mod 16PB
actually i do want the first table also to be updated so i changed the sql to followeing - For Each tdf In CurrentDb.TableDefs
-
If Left(tdf.Name, 4) <> "MSys" Then
-
strsql = "Select t.* From [" & tdf.Name & "] t Inner Join 01_UMWELT On t.fall = [01_UMWELT].fall Where [01_UMWELT].Status = 4"
The [Status] field in the WHERE clause is now 4 instead of 6. No idea why, but it is probably not causing your problem.
but still the first table is not gettin updated. i tried trouble shooting and can see that the code is running through the first table and for ceratin paramerters even though null values exist varii is returning some number and for some fileds even though varii is returning null values update is not taking place but as the loop progresses to next table the code works fine
I'd like to understand what you're saying but I'm afraid you've lost me. I suspect you're doing the right thing by debugging, but once you have the interesting information you need to pass it on to me. I think this is the sticking point at the moment.
i changed the status code for testing purposes but yes that is not creating problem.
one information i have is previously when i was trying to export these tables to excel using vba code (in excel) I noticed that all tables used to get exported correctly but there was a problem in the first table where the first 50 record used to get exported in the middle and not in the order they should be.
similarly here in the recordset i guess initially the first 50 record s are not being considered.
now the problem is updates is occuring in all tables except the first table even when code runs through the first table as i saw it while debugging.
NeoPa 32,556
Expert Mod 16PB
...but there was a problem in the first table where the first 50 record used to get exported in the middle and not in the order they should be.
There is no "order they should be" unless you specify one in a query.
similarly here in the recordset i guess initially the first 50 record s are not being considered.
now the problem is updates is occuring in all tables except the first table even when code runs through the first table as i saw it while debugging.
I need you to explain what you saw or found out while debugging.
During debugging i saw that varii was returning null values for fields in the first table and control was passin through code - Call .Edit
-
.Fields(astrFields(intIx)) = 888
-
Call .Update
but the table is not getting updated.
NeoPa 32,556
Expert Mod 16PB
Now you're talking.
Did ANY of the records, or any of the fields in any record, get updated for this table within your code run?
No none of the records for this table got updated.
updation began only from next table
Also i was trying to get a kind of report in word of the fields getting updated and their record ids
i can manage to get the name of the fields in the word file but how do i get the each id :(
something in word file like
recordid variable existing value updated value
1111 ortsl null value 888
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Colin Steadman |
last post by:
Part No Description Quantity
45643 Random part 10
45678 Another Random part 7
98944 And another 1
<submit button>
...
|
by: Daniel |
last post by:
I use an Access database to basically take data exports, import them,
manipulate the data, and then turn them into exportable reports. I do
this using numerous macros, and queries to get the data...
|
by: Yannick Turgeon |
last post by:
Hello,
We are in the process of examining our current main application. We have to
do some major changes and, in the process, are questionning/validating the
use of MS Access as front-end. The...
|
by: PC Datasheet |
last post by:
I have zero experience with using a SQL database for a backend and Access
for a frontend. I have some questions:
1. Does an SQL database have tables?
2. How does Access connect to the data in...
|
by: Harry Devine |
last post by:
I have a DataGrid that is configured to use the Edit/Update/Cancel concept
correctly. My grid shows values from 5 database fields. I only need to
update that last 4 fields. The last field is a...
|
by: thomasp |
last post by:
First of all, thanks for the help on my previous VB.NET/MS Access questions.
This time I need do the following
1. Connect to a table
2. step through each of its records
3. read the value of two...
|
by: Darren |
last post by:
Help, i want to run an update query from a form..
and was wonderin..
Can the update query run if i want to update a value manually inputted
from a form (e.g. !!) to a table...
|
by: alexandre.brisebois |
last post by:
Hi, I am using access 2003, I would like to know if there is an option
to reorganize the tables in a maner that is readable, as we can do in
sql sever 2000 or 2005.
I have been given a database...
|
by: Ian Davies |
last post by:
Hello
Needing help with a suitable solution.
I have extracted records into a table under three columns 'category',
'comment' and share (the category column also holds the index no of the
record...
|
by: bbasberg |
last post by:
Hello,
I have been struggling with this problem for DAYS and have googled my heart out as well as reading any books I could get my hands on but I cannot find any specific references to my problem....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
| |