how this code works correct

mana

Registered User.
Local time
Today, 08:49
Joined
Nov 4, 2014
Messages
265
hello

i have the following code but it doesn't work do you where my problem is?
i want that where my name field is empty the value of text0 is replaced with it.
thank you

Code:
Dim fldEnumeratord As Object
Dim fldColumnsd As Object
    
Dim dbsd As Database
Dim recsd As Recordset
Set dbsd = CurrentDb
Set recsd = dbsd.OpenRecordset("checks_projects")
Set fldColumnsd = recsd.Fields
    ' Scan the records from beginning to each
    While Not recsd.EOF
        ' Check the current column
        For Each fldEnumeratord In recsd.Fields
            ' If the column is named Title
            If fldEnumeratord.Name = "Project name" Then
                ' If the title of the current record is "Congo"
                If fldEnumeratord.Value = "" Then
                    ' then change its value
                    recsd.Edit
                    recsd("[Project name]").Value = Me.Text0
                    recsd.Update
                End If
            End If
        Next
        ' Move to the next record and continue the same approach
        recsd.MoveNext
    Wend
    recsd.Close
    
    Set recsd = Nothing
    Set dbsd = Nothing
 
when you say 'it doesn't work' what do you mean? - you get a compilation error? it doesn't do anything? you get a wrong result? or what?

have you tried debugging? stepping through the code?

Please be clearer about what the problem is and what you have tried and can confirm what is working e.g. is recsd populated with any records
 
actually there is no error message but it it didn't do anything
and i don't know why
can you help please?
where is my problem?
 
theres no need to recursed through all field when you can do:

if recsd("Project Name").Value & "" = "" Then
.edit
recsd("Project Name") = Me.Text0
.update
end if
 
I suggest putting a few Debug.Print statements in and then show us what you are getting in the immediate window when you run this. I put one in below as an example

Code:
Dim fldEnumeratord As Object
Dim fldColumnsd As Object
    
Dim dbsd As Database
Dim recsd As Recordset
Set dbsd = CurrentDb
Set recsd = dbsd.OpenRecordset("checks_projects")
Set fldColumnsd = recsd.Fields
    ' Scan the records from beginning to each
    While Not recsd.EOF
        ' Check the current column
        For Each fldEnumeratord In recsd.Fields
            ' If the column is named Title
             Debug.Print "fldEnumeratord.Name: " & fldEnumeratord.Name 
            If fldEnumeratord.Name = "Project name" Then
                ' If the title of the current record is "Congo"
                If fldEnumeratord.Value = "" Then
                    ' then change its value
                    recsd.Edit
                    recsd("[Project name]").Value = Me.Text0
                    recsd.Update
                End If
            End If
        Next
        ' Move to the next record and continue the same approach
        recsd.MoveNext
    Wend
    recsd.Close
    
    Set recsd = Nothing
    Set dbsd = Nothing
 
theres no need to recursed through all field when you can do:

if recsd("Project Name").Value & "" = "" Then
.edit
recsd("Project Name") = Me.Text0
.update
end if

Will this replace all the records where the value is empty to the value of that textbox ? Or just the first one ?
 
This seems like a very long way to update some records. I would use a simple SQL statement and execute it.

Code:
CurrentDb.Execute("UPDATE [checks_projects] SET [Project name] = '" & Me.Text0 & _
    "' WHERE [Project name] = '' OR [Project name] IS NULL", dbSeeChanges)
 
thank you for the replies

but i tried this and it didn't work, it has an error message. it needs =

CurrentDb.Execute("UPDATE [checks_projects] SET [Project name] = '" & Me.Text0 & _
"' WHERE [Project name] = '' OR [Project name] IS NULL", dbSeeChanges)


i also tried this but it replaces just the first one and it doesn't work for te others

if recsd("Project Name").Value & "" = "" Then
.edit
recsd("Project Name") = Me.Text0
.update
end if
 
i also put the debug.print and it runned without any error messages and there were no results there , do you know how i can fix it?
 
thank you for the replies

but i tried this and it didn't work, it has an error message. it needs =

CurrentDb.Execute("UPDATE [checks_projects] SET [Project name] = '" & Me.Text0 & _
"' WHERE [Project name] = '' OR [Project name] IS NULL", dbSeeChanges)


i also tried this but it replaces just the first one and it doesn't work for te others

if recsd("Project Name").Value & "" = "" Then
.edit
recsd("Project Name") = Me.Text0
.update
end if

What is the error you got with this?
 
i also put the debug.print and it runned without any error messages and there were no results there , do you know how i can fix it?

I'd abandon the code you have and concentrate on getting the update statement working. That seems like a better approach. So let us know what error that is producing.
 

Users who are viewing this thread

Back
Top Bottom