PC Review


Reply
Thread Tools Rate Thread

Trouble with .AddNew and .Edit

 
 
M
Guest
Posts: n/a
 
      25th May 2004
I have the following code:

Sub AppendFromTxt()

Dim lngI As Long
Dim strBuffer As String
Dim intFileNumber As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset

intFileNumber = FreeFile

Open "C:\Temp.txt" For Input As #intFileNumber

'***Get rid of the first 5 lines

For lngI = 1 To 5

Line Input #intFileNumber, strBuffer

Next lngI

Do Until EOF(intFileNumber)

Line Input #intFileNumber, strBuffer

Set db = CurrentDb
Set rs = db.OpenRecordset("tblTemp")

With rs

Do Until .EOF

.AddNew

If IsNull(.Fields("Field1").Value)
Or .Fields("Field1").Value = "" Then

.Fields("Field1").Value = strBuffer

End If

.MoveNext

Loop

End With

Loop

Close #intFileNumber

End Sub

....that opens the textfile, eliminates the first five
lines as junk and attempts to insert the text into a temp
table. It loops through each line fine and strBuffer
changes with each loop. But it doesn't write to the
Recordset. I'm missing something very simple here. What is
it?!?

Thanks

M
 
Reply With Quote
 
 
 
 
Cheryl Fischer
Guest
Posts: n/a
 
      25th May 2004
M,

I do not see anywhere that you have used the .Update method with the
..AddNew -- it's required when you want to write a new record or change an
existing record using the .Edit method. Try putting it before the line
containing the .MoveNext method.

Also, you might want to try moving the following two lines:

> Set db = CurrentDb
> Set rs = db.OpenRecordset("tblTemp")


so that they come before the execution of your first loop, for example:

> Set db = CurrentDb
> Set rs = db.OpenRecordset("tblTemp")


> Do Until EOF(intFileNumber)
>
> Line Input #intFileNumber, strBuffer





hth,
--

Cheryl Fischer, MVP Microsoft Access



"M" <(E-Mail Removed)> wrote in message
news:1210301c44257$a556e5a0$(E-Mail Removed)...
> I have the following code:
>
> Sub AppendFromTxt()
>
> Dim lngI As Long
> Dim strBuffer As String
> Dim intFileNumber As Integer
> Dim db As DAO.Database
> Dim rs As DAO.Recordset
>
> intFileNumber = FreeFile
>
> Open "C:\Temp.txt" For Input As #intFileNumber
>
> '***Get rid of the first 5 lines
>
> For lngI = 1 To 5
>
> Line Input #intFileNumber, strBuffer
>
> Next lngI
>
> Do Until EOF(intFileNumber)
>
> Line Input #intFileNumber, strBuffer
>
> Set db = CurrentDb
> Set rs = db.OpenRecordset("tblTemp")
>
> With rs
>
> Do Until .EOF
>
> .AddNew
>
> If IsNull(.Fields("Field1").Value)
> Or .Fields("Field1").Value = "" Then
>
> .Fields("Field1").Value = strBuffer
>
> End If
>
> .MoveNext
>
> Loop
>
> End With
>
> Loop
>
> Close #intFileNumber
>
> End Sub
>
> ...that opens the textfile, eliminates the first five
> lines as junk and attempts to insert the text into a temp
> table. It loops through each line fine and strBuffer
> changes with each loop. But it doesn't write to the
> Recordset. I'm missing something very simple here. What is
> it?!?
>
> Thanks
>
> M



 
Reply With Quote
 
M
Guest
Posts: n/a
 
      25th May 2004
I knew it was something simple. Here's what worked:

Set db = CurrentDb
Set rs = db.OpenRecordset("tblTemp")

Do Until EOF(intFileNumber)

Line Input #intFileNumber, strBuffer

With rs

.AddNew

If IsNull(.Fields("Field1").Value)
Or .Fields("Field1").Value = "" Then

.Fields("Field1").Value = strBuffer

End If

.Update

End With

Loop

Close #intFileNumber

End Sub

....I eliminated the second loop and moved the db/rs
statements as you advised. Thanks for your help!

M



>-----Original Message-----
>M,
>
>I do not see anywhere that you have used the .Update

method with the
>..AddNew -- it's required when you want to write a new

record or change an
>existing record using the .Edit method. Try putting it

before the line
>containing the .MoveNext method.
>
>Also, you might want to try moving the following two

lines:
>
>> Set db = CurrentDb
>> Set rs = db.OpenRecordset("tblTemp")

>
>so that they come before the execution of your first

loop, for example:
>
>> Set db = CurrentDb
>> Set rs = db.OpenRecordset("tblTemp")

>
>> Do Until EOF(intFileNumber)
>>
>> Line Input #intFileNumber, strBuffer

>
>
>
>
>hth,
>--
>
>Cheryl Fischer, MVP Microsoft Access
>
>
>
>"M" <(E-Mail Removed)> wrote in message
>news:1210301c44257$a556e5a0$(E-Mail Removed)...
>> I have the following code:
>>
>> Sub AppendFromTxt()
>>
>> Dim lngI As Long
>> Dim strBuffer As String
>> Dim intFileNumber As Integer
>> Dim db As DAO.Database
>> Dim rs As DAO.Recordset
>>
>> intFileNumber = FreeFile
>>
>> Open "C:\Temp.txt" For Input As #intFileNumber
>>
>> '***Get rid of the first 5 lines
>>
>> For lngI = 1 To 5
>>
>> Line Input #intFileNumber, strBuffer
>>
>> Next lngI
>>
>> Do Until EOF(intFileNumber)
>>
>> Line Input #intFileNumber, strBuffer
>>
>> Set db = CurrentDb
>> Set rs = db.OpenRecordset("tblTemp")
>>
>> With rs
>>
>> Do Until .EOF
>>
>> .AddNew
>>
>> If IsNull(.Fields("Field1").Value)
>> Or .Fields("Field1").Value = "" Then
>>
>> .Fields("Field1").Value = strBuffer
>>
>> End If
>>
>> .MoveNext
>>
>> Loop
>>
>> End With
>>
>> Loop
>>
>> Close #intFileNumber
>>
>> End Sub
>>
>> ...that opens the textfile, eliminates the first five
>> lines as junk and attempts to insert the text into a

temp
>> table. It loops through each line fine and strBuffer
>> changes with each loop. But it doesn't write to the
>> Recordset. I'm missing something very simple here. What

is
>> it?!?
>>
>> Thanks
>>
>> M

>
>
>.
>

 
Reply With Quote
 
Marshall Barton
Guest
Posts: n/a
 
      25th May 2004
M wrote:

>I have the following code:
>
>Sub AppendFromTxt()
>
>Dim lngI As Long
>Dim strBuffer As String
>Dim intFileNumber As Integer
>Dim db As DAO.Database
>Dim rs As DAO.Recordset
>
>intFileNumber = FreeFile
>
>Open "C:\Temp.txt" For Input As #intFileNumber
>
>'***Get rid of the first 5 lines
>
>For lngI = 1 To 5
>
> Line Input #intFileNumber, strBuffer
>
>Next lngI
>
>Do Until EOF(intFileNumber)
>
> Line Input #intFileNumber, strBuffer
>
> Set db = CurrentDb
> Set rs = db.OpenRecordset("tblTemp")
>
> With rs
>
> Do Until .EOF
>
> .AddNew
>
> If IsNull(.Fields("Field1").Value)
>Or .Fields("Field1").Value = "" Then
>
> .Fields("Field1").Value = strBuffer
>
> End If
>
> .MoveNext
>
> Loop
>
> End With
>
>Loop
>
>Close #intFileNumber
>
>End Sub
>
>...that opens the textfile, eliminates the first five
>lines as junk and attempts to insert the text into a temp
>table. It loops through each line fine and strBuffer
>changes with each loop. But it doesn't write to the
>Recordset. I'm missing something very simple here. What is
>it?!?



Actually you've several problems with that code logic. The
one you're asking for is that you have to use the .Update
method to save the new record.

However, if you fix that, then you'r going to get a real
mess because of the loop until eof. The way you have it,
you're trying to add a new record for every record that
you've previously added (which doesn't make sense to me).

I also don't understand why you then check if the new record
has a Null value in a field and of course it does because
nothing has been inserted yet.

This whole approach is going to be rather slow to execute,
so if you don't have a good reason that you haven't yet
explained, why don't you just use TransferText to import the
text file into the table (and then delete the unwanted five
records)??
--
Marsh
MVP [MS Access]
 
Reply With Quote
 
M
Guest
Posts: n/a
 
      25th May 2004
You're totally right --- I normally do these things that
way. Just trying a new approach.

Thanks for your advice.

M


>-----Original Message-----
>M wrote:
>
>>I have the following code:
>>
>>Sub AppendFromTxt()
>>
>>Dim lngI As Long
>>Dim strBuffer As String
>>Dim intFileNumber As Integer
>>Dim db As DAO.Database
>>Dim rs As DAO.Recordset
>>
>>intFileNumber = FreeFile
>>
>>Open "C:\Temp.txt" For Input As #intFileNumber
>>
>>'***Get rid of the first 5 lines
>>
>>For lngI = 1 To 5
>>
>> Line Input #intFileNumber, strBuffer
>>
>>Next lngI
>>
>>Do Until EOF(intFileNumber)
>>
>> Line Input #intFileNumber, strBuffer
>>
>> Set db = CurrentDb
>> Set rs = db.OpenRecordset("tblTemp")
>>
>> With rs
>>
>> Do Until .EOF
>>
>> .AddNew
>>
>> If IsNull(.Fields("Field1").Value)
>>Or .Fields("Field1").Value = "" Then
>>
>> .Fields("Field1").Value = strBuffer
>>
>> End If
>>
>> .MoveNext
>>
>> Loop
>>
>> End With
>>
>>Loop
>>
>>Close #intFileNumber
>>
>>End Sub
>>
>>...that opens the textfile, eliminates the first five
>>lines as junk and attempts to insert the text into a

temp
>>table. It loops through each line fine and strBuffer
>>changes with each loop. But it doesn't write to the
>>Recordset. I'm missing something very simple here. What

is
>>it?!?

>
>
>Actually you've several problems with that code logic.

The
>one you're asking for is that you have to use the .Update
>method to save the new record.
>
>However, if you fix that, then you'r going to get a real
>mess because of the loop until eof. The way you have it,
>you're trying to add a new record for every record that
>you've previously added (which doesn't make sense to me).
>
>I also don't understand why you then check if the new

record
>has a Null value in a field and of course it does because
>nothing has been inserted yet.
>
>This whole approach is going to be rather slow to execute,
>so if you don't have a good reason that you haven't yet
>explained, why don't you just use TransferText to import

the
>text file into the table (and then delete the unwanted

five
>records)??
>--
>Marsh
>MVP [MS Access]
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Addnew followed by Edit MIG Microsoft Access Forms 2 27th Jul 2010 12:31 PM
update or cancelupdate without addnew or edit =?Utf-8?B?WWlzTWFu?= Microsoft Access VBA Modules 5 10th Apr 2006 09:15 PM
Edit or AddNew in a Form =?Utf-8?B?SmFtZXM=?= Microsoft Access Getting Started 2 5th Oct 2005 12:04 AM
DAO AddNew/Edit Methods Ronald Dodge Microsoft Access VBA Modules 4 5th Jan 2004 04:02 PM
Update or CancelUpdate without Addnew or Edit Synergy Microsoft Access VBA Modules 1 6th Oct 2003 07:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 PM.