Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone show me how to write this simple function?

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Apr 1, 2003
342
GB

Hi,

I have never written vba / functions before, but I would like to learn by seeing how the code would work for a problem I need to solve.

The table is called 'Books' and the column I need to update is called 'processed' (all in one simple database).


Basically i need to write a function that when run, adds the word "complete" to the processed column (for all records) IF the word 'notcomplete' does not exist.

sounds simple enough, but when you've never written VBA before its not!

many thanks.
 
So after the last filled cell in this column, you want to stick "COMPLETE" in the next empty cell?

On Sheet1, in cell T1, write
=COUNTA(A:A)
or if it's column F with your data, write
=COUNTA(F:F)

This will count each item in that column so we can reference it and add to the next empty cell. You can hide it if you want. It's referred to below as (1,20). You'll need to change that if you change the cell you write it in.
[tt]
Public Lists as Object
Set Lists = Sheets("Sheet1").Cells

Private btnButton_Click()
Dim Row as Integer
Row = Lists(1, 20) + 1

If Lists(Row, 1) = "NOT COMPLETE" Then
Exit Sub
Else
Lists(Row, 1) = "COMPLETE"
End If
End Sub
[/tt]

I haven't tested this, but hope it helps.
 
Thanks for this - I'm not sure but is this for Excel? I should have said it was for a table in Access - how would it differ?

thanks again.
 
Yes for Excel. Sorry, didn't realise you were in Access.
 
Sorry I wasted your time there. I mentioned it was for a database, but I should have been clearer - and i suppose excel is just a simple database!

cheers anyway.

P.S. can anyone show me the code for Access?
 
I don't know if this is the "accepted" approach do do what you want, but it seems to work:
[blue]
Code:
Sub UpdateBooks()
Dim q As QueryDef
Dim sSqlStatement As String
  sSqlStatement = "UPDATE Books SET Books.processed = 'complete' " _
    + &quot; WHERE (Nz([Books].[processed],' ')<>'notcomplete');&quot;
  Set q = CurrentDb.CreateQueryDef(&quot;Temp&quot;, sSqlStatement)
  q.Execute
  CurrentDb.QueryDefs.Delete (&quot;Temp&quot;)
  Set q = Nothing
End Sub
[/color]

You might want to post your request again in one of the Access forums.
 
Thanks - I guess there will be more than one way to write this.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top