Results 1 to 10 of 10
  1. #1
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6

    Get value from one field, and paste it in to a new column

    Hello everyone!

    I'm relatively new to Microsoft Access, and just a beginner with SQL programming.

    I have a table in MS Access. It is an import from a .txt file. In the first column There is a String with a Number or Code in it. This number always Comes after a string "Arbeitslpatz "(The first character starts at string Position 17) and always changes every 76 rows. (Total rows 29790). What I want to achiev is to copy this number (for example 101100) and paste it in a new column in the first 76 rows. Then comes the next number for the next 76 rows, and so on. (I later want to to a lookup function with SQL WHERE functions and return some desired values, and for that I need to have these "Arbeitsplatz numbers" in a new row.
    I already assigned an ID to the file, but I didn't manage to get farther. (I experimented with VBA Loops, but didn' really succeed)
    Either SQL or VBA Code would be appreciated.


    Click image for larger version. 

Name:	Screenshot (3).jpg 
Views:	11 
Size:	121.7 KB 
ID:	39110 Click image for larger version. 

Name:	Screenshot (6).jpg 
Views:	11 
Size:	126.7 KB 
ID:	39111
    I don't know how well the screenshots can be seen &/ if I provided enough Information...

    Thank you all for your help

  2. #2
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,563
    Hi

    It would be far easier if you Copy the .txt File into Excel and then carry out a Text to Columns Process.

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    easiest to use a function - something like

    Code:
    function getCode(s as string) as string
    static lastCode as string
    
        if s like "Arbeitslpatz*" then   lastCode=split(mid(s,17)," ")(0)
        getCode=lastCode
    
    end if
    then in your query, something like

    SELECT getCode([Arbeitslpatz]),

  4. #4
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6
    Thanks for all the tips!

  5. #5
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6
    Quote Originally Posted by Neymar0001 View Post
    I know that, but unfortunately that is not an option for me. I have to update the file daily, and don't want to do it always manually. But thanks for your tip anyway
    @mike60smart

  6. #6
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6
    Quote Originally Posted by Ajax View Post
    easiest to use a function - something like

    Code:
    function getCode(s as string) as string
    static lastCode as string
    
        if s like "Arbeitslpatz*" then   lastCode=split(mid(s,17)," ")(0)
        getCode=lastCode
    
    end if
    then in your query, something like

    SELECT getCode([Arbeitslpatz]),
    I copied your code in the VBA Module in access. I then created a query, and tried to run this code> SELECT getCode([CM01_Total]![Arbeitsplatz]); I receive an error message which says undifined function 'getCode' in expression. What did I do wrong? Thanks in advance!

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    doesn't look like you completed your query

    and to be clear the getCode function needs to be in a standard vba module (with a different name such as modString), not a form or class module

  8. #8
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6
    Quote Originally Posted by Ajax View Post
    doesn't look like you completed your query

    and to be clear the getCode function needs to be in a standard vba module (with a different name such as modString), not a form or class module
    I almost succeded...

    This is the slightly modified VBA code:

    Code:
    Option Compare Database
    Function modString(s As String) As String
    Static lastCode As String
        If s Like "Arbeitsplatz*" Then lastCode = Split(mi(s, 17), " ")(0)
        modString = lastCode
        End If
    End Function
    And the SQL:

    Code:
    SELECT modString([Arbeitsplatz]) AS Expr1, *
    FROM CM01_Total;
    I get this error message:
    There was an error compiling this function.
    The Visual Basic module contains a sytax error.
    Check the code, and the recompile it.


    Thank you for your help!

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    Split(mi(s, 17), " ")(0)

    recommend

    a. Put Option Explicit at the top of each module which will capture errors like this
    b. when you written your code, compile it (debug>compile on the vba ribbon

    Just out of curiositity why did you not just copy/paste my code from the thread?- it was complete and did not need any changes

  10. #10
    Neymar0001 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2019
    Posts
    6
    I finally found a sollution. It works with the original .txt file, and imports it as I want it.

    VBA Code:

    Code:
    Option Compare Database
    Function Macro1()
    '' dim variables
    Dim tmp As String, temp() As String
    Dim rs As Recordset
    '' open source for reading
    Open "My\File\Location\File.txt" For Input As #1
    '' attach recordset to table
    Set rs = CurrentDb.OpenRecordset("Target_Table")
    '' loop source - read line-by-line until its end
    Do Until EOF(1)
      '' read one line
      Line Input #1, tmp
      '' look if it is a header, if yes, extract and store the number
      If Left(tmp, 12) = "Arbeitsplatz" Then num = Trim(Mid(tmp, 17, 8))
      '' look if it is a data line
      If InStr(tmp, "|") > 0 And InStr(tmp, "Woche") = 0 Then
        '' divide the line to separate fields
        temp = Split(tmp, "|")
        With rs
          '' add new, empty, record
          .AddNew
          '' set values into fields
          !Arbeitsplatz = num
          !Woche = temp(1)
          !Bedarf = temp(2)
          !Angebot = temp(3)
          !Belast = temp(4)
          !Freie_Kap = temp(5)
          !Einh = temp(6)
          '' save editions
          .Update
        End With
      End If
    '' loop to reading the next line
    Loop
    '' close source file and recordset
    Close #1
    rs.Close
    Set rs = Nothing
    End Function
    
    I hope this helps someone (It helped me a LOT)

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

Similar Threads

  1. paste excel column data into query?
    By ruiter in forum Access
    Replies: 21
    Last Post: 05-02-2016, 10:39 AM
  2. Replies: 2
    Last Post: 01-20-2016, 08:43 AM
  3. copy and paste part of a field
    By Jen0dorf in forum Access
    Replies: 2
    Last Post: 01-18-2016, 02:57 AM
  4. Replies: 4
    Last Post: 12-19-2013, 01:18 PM
  5. Copy and Paste non-visable field
    By delap009 in forum Forms
    Replies: 2
    Last Post: 08-22-2013, 10:23 AM

Tags for this Thread

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