Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    VBA for input box

    I have created a macro after some days. All it does it create an audit record of specific changes I want to track.



    I have an example database which uses VBA to do the same thing. On this database there is some code which forces the user to enter their 'reason for change'.

    This information is then kept alongside the new record in the audit table.

    All I need is an input box that opens either when a text box is updated OR when a new record is added to a table. The text should then be saved accordingly.

    Relevant information:

    form [JobF] contains the textbox [Scheduled_Start] < this is what can be changed and needs to b tracked
    [AuditT] is the table where the new records will be kept.

    all help appreciated.

  2. #2
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Code:
    Private Sub Scheduled_Start_AfterUpdate()
    Dim Note As String
        Note = InputBox("Reason for change:", _
                 "Rescheduling Tracker")
        
    End Sub
    This is what I have so far. As an input box it's working. I just cant figure out how to save the input.

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    First, I would say it is a good idea to use Hungarian Notation with your variable names. One reason why is to avoid reserved words. I do not know if Notes is a reserved word, but strNotes is not a reserved word.

    What do you want to do with the user input? You can edit an existing record in a table using SQL.
    Code:
    Dim strSQL As String
     Dim strNote As String
         strNote = InputBox("Reason for change:", _
                  "Rescheduling Tracker")
     strSQL = "UPDATE TableName SET TableName.FieldName = '" & strNote & "' WHERE TableName.MyFieldID = 5"
    CurrentDb.Execute strSQL

  4. #4
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    *Googles Hungarian notation* - As with everything I do; I'm just trying out suggestions or what I find online/in books. I had never even heard of Hungarian notation. I do see your point. Thanks for that it does look a lot better.

    Just a question about the code;

    Code:
    WHERE TableName.MyFieldID = 5
    Why does it =5? how do I change this value to represent the active record.
    I'll have a play and see what I can do.

    I have made a macro which creates the audit record, I want the user entered note to be added to the last record added to the audit table. Am I asking too much with this?

  5. #5
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    attachment.zip

    This is what I'm using to reference, although my method is very different to how this works. This has an input box that comes up and then applies the user entered 'note' to the audit.

    Currently my macro will enter [new data] [old data] [when]. I just need to add a note for the reason of change.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Why does it =5? how do I change this value to represent the active record.
    You would replace 5 with a variable. First declare a variable and then use the variable by concatenating to your WHERE clause.
    Code:
    Dim strSQL As String
     Dim strNote As String
    dim lngFieldID as long
         
         lngFieldID = 5
         strNote = InputBox("Reason for change:", _
                  "Rescheduling Tracker")
    
     strSQL = "UPDATE TableName SET TableName.FieldName = '" & strNote & "' WHERE TableName.MyFieldID = " & lngFieldID
    CurrentDb.Execute strSQL
    I have made a macro which creates the audit record, I want the user entered note to be added to the last record added to the audit table. Am I asking too much with this?
    If you use an embedded macro or a saved Macro Object to append a record, it is not likely you will know this record's PK value. So, determining what value to replace 5 with will be difficult. Why not replace the UPDATE statement in my previous example with an append query? This is why I will suggest creating a Query Object that performs the task you desire. You can name and save the Query Object or use the SQL statement generated by the query designer in your VBA.

    This is what I'm using to reference, although my method is very different to how this works. This has an input box that comes up and then applies the user entered 'note' to the audit.
    I avoid Macros when possible. They may seem easier to implement, but the reality is they have limits. When these limits are realized, the real work begins. Also, the use of input boxes seems like a good idea, but they rarely are. Before assigning a value to a field in a table, you need to do data validation. It is common practice to use Controls within forms to collect user input. I prefer to do data validation on controls.

    I downloaded your example. I do not see any Macros. In Access, a Macro is an Object. When you record a macro in Excel, it is saved as VBA. When you record a macro in Access, it is saved as an Object and it has a Name. What you have in your example is not a Named and Saved Macro Object. You have some functions that create SQL statements and execute the statements. These functions are VBA procedures.

    I did not look very carefully at the custom functions, but it seems you would have to make edits to the function in order to accept arguments for multiple columns. I would recommend using the approach I suggested earlier. Create an action query using the Query Designer.

  7. #7
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Im actually understanding code/variables now. Not totally, but a bit.... Its a start. Thanks mate.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I don't know if I have mentioned them to you before, but I created a few video tutorials that are available for free download. There are a several on VBA. I would recommend watching the VBA videos in order. The one's regarding the Message Box are a good introduction to Functions.
    https://www.accessforums.net/tutoria...tml#post277671

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 09-23-2015, 02:29 PM
  2. Replies: 1
    Last Post: 11-04-2014, 12:07 PM
  3. Replies: 6
    Last Post: 10-27-2014, 08:05 PM
  4. Replies: 5
    Last Post: 01-29-2014, 02:42 PM
  5. Input Forms - How To Input Multiple Fields/Records?
    By butterbescotch in forum Forms
    Replies: 1
    Last Post: 04-04-2013, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums