Please help me edit this code

G

Gersh76

I want to change this code so that it will only check the records that I
specify, not every record in the table. Here is the code, can anyone help?

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

Dim tbl As Variant
Dim newnotes As String
Dim i As Integer

Set tbl = CurrentDb.OpenRecordset("Select * from Body")

If tbl.EOF Then
MsgBox "No Squares Found"
Exit Sub
Else
tbl.MoveFirst
i = 0
Do Until tbl.EOF
If Not IsNull(tbl.Notes) Then
newnotes = Replace(tbl.Notes, vbCrLf, "aabbccbbaa")
newnotes = Replace(newnotes, vbLf, vbCrLf)
newnotes = Replace(newnotes, "aabbccbbaa", vbCrLf)
i = i + 1
If tbl.Notes = newnotes Then
MsgBox "Record " & i & " not changed"
Else
MsgBox "RECORD " & i & " CHANGED"
End If

tbl.Edit
tbl.Notes = newnotes
tbl.Update
End If

tbl.MoveNext
Loop
End If



Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub
 
G

Gersh76

Right now its using a button on a form to click through the records
one-by-one. Any suggestions of a better way?
 
J

John Spencer

Why not just use an update query that looks like the following.

UPDATE Body
SET NewNotes =
Replace(Replace([NewNotes],Chr(13) & Chr(10),Chr(10)),Chr(10),Chr(13) &
Chr(10))
WHERE NewNotes Like "*" & Chr(10) & "*"

My assumption (you didn't say) is that you want to do this for all
records that have a Chr(10) in the record's NewNotes field. If you have
other criteria, you might want to say what the criteria are.

The inner replace removes all the chr(13) and the outer Replace then
adds chr(13) onto all the chr(10).
'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 
J

JonWayn

My guess is that you wish to replace every vbLf that is not part of a vbCrLf
with vbCrLf. If that is the case, where I see you going wrong is where (the
location at which) you update the record. That should have been done within
the If block that tests to see if the record was changed, following the Else
clause: as in the following

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

Dim tbl As Variant
Dim newnotes As String
Dim i As Integer

Set tbl = CurrentDb.OpenRecordset("Select * from Body")

If tbl.EOF Then
MsgBox "No Squares Found"
Exit Sub
Else
tbl.MoveFirst
i = 0

Do Until tbl.EOF
If Not IsNull(tbl.Notes) Then
newnotes = Replace(tbl.Notes, vbCrLf, "aabbccbbaa")
newnotes = Replace(newnotes, vbLf, vbCrLf)
newnotes = Replace(newnotes, "aabbccbbaa", vbCrLf)
i = i + 1
If tbl.Notes = newnotes Then
MsgBox "Record " & i & " not changed"
Else
MsgBox "RECORD " & i & " CHANGED"
'**** place update statements here, otherwise all non-null
records will get updated ***
tbl.Edit
tbl.Notes = newnotes
tbl.Update
End If
End If

tbl.MoveNext
Loop
End If



Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub
 
J

JonWayn

Also, this is tangential to the issue at hand but, to refer to a recordset's
field value, the notation Recordset!FieldValue is what is documented. I
really am not sure if there are downsides to using the notation
Recordset.FieldValue (note the use of "!" , as opposed to "." )
 
G

Gersh76

I guess I should be a little more clear about what I'm looking for. What I'm
doing is pasting from Excel to Access. The text in Excel has line spaces,
but when pasted into Access, the line spaces disappear and are replaced by a
little box. The code from my original post is used to replace these boxes
with line spaces. The code is used with a button on a form to go through all
the records one-by-one and fix the problem. I push the button once, it
reports back if it fixed it or not, then I push the button again and it
checks the next record, and so on. The database I'll be using it in has tens
of thousands of records, so I don't want to have to click through them all
everytime. I want to change this code or have someone write me a new one
that I can use to either fix only new records or specified records. It may
also work if it just ran through all the records with one click instead of
having to click every one. I did not write this code and really don't know
anything about it, so any help is greatly appreciated and please write back
as if you were writing to somebody that knows nothing about this code.
Thanks.
 
J

JonWayn

I think this should work. When you click the Command0 button, all occurrences
of linefeeds that are not preceeded by a carriage return will be replaced
with carriage-return-linefeed characters. In other words, the little boxes
that you mention in your post will be replaced with line-breaks. Note,
however, that line-breaks are displayed by 2 little boxes.

Private Sub Command0_Click()
Dim Cnt%
Const SomeAlienText$ = "~`~`~`~"
Const C$ = vbCr, L$ = vbLf, CL$ = vbCrLf

Cnt = DCount("*", Tbl, "[Notes] Like '*[!" & C & "]" & L & "*'")

If Cnt > 0 then
MsgBox "About to replace " & Cnt & " linefeed occurrence(s) with
Carriage-Return+Linefeed"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbCrLf & "', '" &
SomeAlienText & "', TRUE) " _
& "WHERE [SomeField] Like '*" & vbCrLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbLf & "', '" & vbCrLf &
"', TRUE) " _
& "WHERE [SomeField] Like '*" & vbLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & SomeAlienText & "', '" &
VbCrLf & "') " _
& "WHERE [SomeField] Like '*" & SomeAlienText & "*'"
Else
MsgBox "No new Linefeed occurrences were found"
Endif
EEnd Sub


Function ReplaceSubString$(ByVal MainString$, ByVal sFind$, ByVal sReplace$, _
Optional ByVal Recursive As Boolean = False)
'Searches MainText for, and replaces, FindText, with ReplaceText. Replaces
only 1st, or all instance(s) -
' based on Recursive. Replaces whole and partial words
Dim sBefore$, sAfter$
Dim iFindLen%, iFindIndex%

iFindLen = Len(sFind)

Do Until InStr(1, MainString, sFind, 1) = 0
iFindIndex = InStr(1, MainString, sFind, 1)
sBefore = Mid(MainString, 1, iFindIndex - 1)
sAfter = Mid(MainString, iFindIndex + iFindLen)
MainString = sBefore & sReplace & sAfter
If Not Recursive Then Exit Do
Loop

ReplaceSubString = MainString
End Function
 
J

John Spencer

As I posted before this can all be done with ONE query. If you want to
limit the records that are updated to a specific set, then you need some way
to identify the new records (or the set of records you want to update). Do
the records that you are pasting have some unique value that identifies them
as a set. - a date field which contains (for instance) the current date?
Or perhaps you are using a sequential autonumber field as the primary key?

If you have some method to specifically identify the pasted records then all
you need to do is add the criteria to the query..

UPDATE Body
SET NewNotes =
Replace(Replace([NewNotes],Chr(13) & Chr(10),Chr(10)),Chr(10),Chr(13) &
Chr(10))
WHERE NewNotes Like "*" & Chr(10) & "*"

The inner replace removes all the chr(13) and the outer Replace then
adds chr(13) onto all the chr(10).

If you have no specific way to identify the new records, then I might do
this in several steps.
-- paste the new records into a work table that is a copy of your current
table with NO records in it.
-- run the update query against the Body_WorkTable
-- Copy the records from Body_work into Body table.
-- Delete the records from Body_Work table in preparation for the next
effort.

If you only know how to build a query using the design view then here are
the rough steps to do so.
-- Select your Body table
-- Add NewNotes field to the query
-- Under new notes in the criteria section enter
LIKE "*" & Chr(10) & "*"
-- Select Query : UPdate from the menu
-- in the Update to under new notes type (all one line)
Replace(Replace([NewNotes],Chr(13) & Chr(10),Chr(10)),Chr(10),Chr(13) &
Chr(10))

--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
G

Gersh76

When I tried to use this code I got an error "compile error: syntax error".
It highlighted the first line in yellow and also highlighted
"Carriage-Return+Linefeed. Any idea what's wrong? Thanks.

JonWayn said:
I think this should work. When you click the Command0 button, all occurrences
of linefeeds that are not preceeded by a carriage return will be replaced
with carriage-return-linefeed characters. In other words, the little boxes
that you mention in your post will be replaced with line-breaks. Note,
however, that line-breaks are displayed by 2 little boxes.

Private Sub Command0_Click()
Dim Cnt%
Const SomeAlienText$ = "~`~`~`~"
Const C$ = vbCr, L$ = vbLf, CL$ = vbCrLf

Cnt = DCount("*", Tbl, "[Notes] Like '*[!" & C & "]" & L & "*'")

If Cnt > 0 then
MsgBox "About to replace " & Cnt & " linefeed occurrence(s) with
Carriage-Return+Linefeed"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbCrLf & "', '" &
SomeAlienText & "', TRUE) " _
& "WHERE [SomeField] Like '*" & vbCrLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbLf & "', '" & vbCrLf &
"', TRUE) " _
& "WHERE [SomeField] Like '*" & vbLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & SomeAlienText & "', '" &
VbCrLf & "') " _
& "WHERE [SomeField] Like '*" & SomeAlienText & "*'"
Else
MsgBox "No new Linefeed occurrences were found"
Endif
EEnd Sub


Function ReplaceSubString$(ByVal MainString$, ByVal sFind$, ByVal sReplace$, _
Optional ByVal Recursive As Boolean = False)
'Searches MainText for, and replaces, FindText, with ReplaceText. Replaces
only 1st, or all instance(s) -
' based on Recursive. Replaces whole and partial words
Dim sBefore$, sAfter$
Dim iFindLen%, iFindIndex%

iFindLen = Len(sFind)

Do Until InStr(1, MainString, sFind, 1) = 0
iFindIndex = InStr(1, MainString, sFind, 1)
sBefore = Mid(MainString, 1, iFindIndex - 1)
sAfter = Mid(MainString, iFindIndex + iFindLen)
MainString = sBefore & sReplace & sAfter
If Not Recursive Then Exit Do
Loop

ReplaceSubString = MainString
End Function



Gersh76 said:
I guess I should be a little more clear about what I'm looking for. What I'm
doing is pasting from Excel to Access. The text in Excel has line spaces,
but when pasted into Access, the line spaces disappear and are replaced by a
little box. The code from my original post is used to replace these boxes
with line spaces. The code is used with a button on a form to go through all
the records one-by-one and fix the problem. I push the button once, it
reports back if it fixed it or not, then I push the button again and it
checks the next record, and so on. The database I'll be using it in has tens
of thousands of records, so I don't want to have to click through them all
everytime. I want to change this code or have someone write me a new one
that I can use to either fix only new records or specified records. It may
also work if it just ran through all the records with one click instead of
having to click every one. I did not write this code and really don't know
anything about it, so any help is greatly appreciated and please write back
as if you were writing to somebody that knows nothing about this code.
Thanks.
 
J

JonWayn

I see that John Spencer suggests a quick idea. You can use that or use the
following. If you are going to use this code, take care to get the line wraps
correctly, as the newsgroups window may create some of its own. I presume you
know VB enough to be able to remove newsgroups-inserted line breaks if they
occur.

Private Sub Command0_Click()
Dim Cnt%
Const SomeAlienText$ = "~`~`~`~"
Const C$ = vbCr, L$ = vbLf, CL$ = vbCrLf

Cnt = DCount("*", "SomeTable", "[Notes] Like '*[!" & C & "]" & L & "*'")

If Cnt = 0 Then
MsgBox "About to replace " & Cnt & " linefeed occurrence(s) with "
& "Carriage-Return+Linefeed"

DoCmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbCrLf & "', '" &
SomeAlienText & "', TRUE) " _
& "WHERE [SomeField] Like '*" & vbCrLf & "*'"

DoCmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbLf & "', '" & vbCrLf &
"', TRUE) " _
& "WHERE [SomeField] Like '*" & vbLf & "*'"

DoCmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & SomeAlienText & "', '" &
vbCrLf & "') " _
& "WHERE [SomeField] Like '*" & SomeAlienText & "*'"
Else
MsgBox "No new Linefeed occurrences were found"
End If
End Sub


Function ReplaceSubString$(ByVal MainString$, ByVal sFind$, ByVal
sReplace$, _
Optional ByVal Recursive As Boolean = False)
'Searches MainText for, and replaces, FindText, with ReplaceText. Replaces
only 1st, or all instance(s) -
' based on Recursive. Replaces whole and partial words
Dim sBefore$, sAfter$
Dim iFindLen%, iFindIndex%

iFindLen = Len(sFind)

Do Until InStr(1, MainString, sFind, 1) = 0
iFindIndex = InStr(1, MainString, sFind, 1)
sBefore = Mid(MainString, 1, iFindIndex - 1)
sAfter = Mid(MainString, iFindIndex + iFindLen)
MainString = sBefore & sReplace & sAfter
If Not Recursive Then Exit Do
Loop

ReplaceSubString = MainString
End Function


Gersh76 said:
When I tried to use this code I got an error "compile error: syntax error".
It highlighted the first line in yellow and also highlighted
"Carriage-Return+Linefeed. Any idea what's wrong? Thanks.

JonWayn said:
I think this should work. When you click the Command0 button, all occurrences
of linefeeds that are not preceeded by a carriage return will be replaced
with carriage-return-linefeed characters. In other words, the little boxes
that you mention in your post will be replaced with line-breaks. Note,
however, that line-breaks are displayed by 2 little boxes.

Private Sub Command0_Click()
Dim Cnt%
Const SomeAlienText$ = "~`~`~`~"
Const C$ = vbCr, L$ = vbLf, CL$ = vbCrLf

Cnt = DCount("*", Tbl, "[Notes] Like '*[!" & C & "]" & L & "*'")

If Cnt > 0 then
MsgBox "About to replace " & Cnt & " linefeed occurrence(s) with
Carriage-Return+Linefeed"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbCrLf & "', '" &
SomeAlienText & "', TRUE) " _
& "WHERE [SomeField] Like '*" & vbCrLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & vbLf & "', '" & vbCrLf &
"', TRUE) " _
& "WHERE [SomeField] Like '*" & vbLf & "*'"

Docmd.RunSQL "UPDATE [SomeTable] SET [SomeField] = " _
& "ReplaceSubString([SomeField], '" & SomeAlienText & "', '" &
VbCrLf & "') " _
& "WHERE [SomeField] Like '*" & SomeAlienText & "*'"
Else
MsgBox "No new Linefeed occurrences were found"
Endif
EEnd Sub


Function ReplaceSubString$(ByVal MainString$, ByVal sFind$, ByVal sReplace$, _
Optional ByVal Recursive As Boolean = False)
'Searches MainText for, and replaces, FindText, with ReplaceText. Replaces
only 1st, or all instance(s) -
' based on Recursive. Replaces whole and partial words
Dim sBefore$, sAfter$
Dim iFindLen%, iFindIndex%

iFindLen = Len(sFind)

Do Until InStr(1, MainString, sFind, 1) = 0
iFindIndex = InStr(1, MainString, sFind, 1)
sBefore = Mid(MainString, 1, iFindIndex - 1)
sAfter = Mid(MainString, iFindIndex + iFindLen)
MainString = sBefore & sReplace & sAfter
If Not Recursive Then Exit Do
Loop

ReplaceSubString = MainString
End Function



Gersh76 said:
I guess I should be a little more clear about what I'm looking for. What I'm
doing is pasting from Excel to Access. The text in Excel has line spaces,
but when pasted into Access, the line spaces disappear and are replaced by a
little box. The code from my original post is used to replace these boxes
with line spaces. The code is used with a button on a form to go through all
the records one-by-one and fix the problem. I push the button once, it
reports back if it fixed it or not, then I push the button again and it
checks the next record, and so on. The database I'll be using it in has tens
of thousands of records, so I don't want to have to click through them all
everytime. I want to change this code or have someone write me a new one
that I can use to either fix only new records or specified records. It may
also work if it just ran through all the records with one click instead of
having to click every one. I did not write this code and really don't know
anything about it, so any help is greatly appreciated and please write back
as if you were writing to somebody that knows nothing about this code.
Thanks.

:

I want to change this code so that it will only check the records that I
specify, not every record in the table. Here is the code, can anyone help?

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click

Dim tbl As Variant
Dim newnotes As String
Dim i As Integer

Set tbl = CurrentDb.OpenRecordset("Select * from Body")

If tbl.EOF Then
MsgBox "No Squares Found"
Exit Sub
Else
tbl.MoveFirst
i = 0
Do Until tbl.EOF
If Not IsNull(tbl.Notes) Then
newnotes = Replace(tbl.Notes, vbCrLf, "aabbccbbaa")
newnotes = Replace(newnotes, vbLf, vbCrLf)
newnotes = Replace(newnotes, "aabbccbbaa", vbCrLf)
i = i + 1
If tbl.Notes = newnotes Then
MsgBox "Record " & i & " not changed"
Else
MsgBox "RECORD " & i & " CHANGED"
End If

tbl.Edit
tbl.Notes = newnotes
tbl.Update
End If

tbl.MoveNext
Loop
End If



Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top