I have an access table with the fields
ID
Field1
Field2
Field3
I also have a word table with headers
ID | Field1 | Field2 | Field3
How can I import automatically all the data from the table into an assess database from the word file ?
Importing automatically doesnt sound easy, but you could do it programmatically.
From Access, you'd do something like this:
dim base as string: base = "INSERT INTO tblname (ID, Field1, Field2, Field3) VALUES ("
dim sql as string
dim ii as long
dim jj as long
dim wrd as object
dim wrdDoc as object
'
docmd.setwarnings false
set wrd = createObject("Word.Application")
wrd.visible = false
set wrdDoc = wrd.Documents.Add("name of word document containing table")
with wrdDoc.Tables(1) 'assuming first table in document
for ii = ? to .Rows.Count 'if the table has column headings, ? = 2, else 1
sql = base
for jj = 1 to 4 '4 = count of columns
sql = sql & iif(jj = 1, "", ",") & CStr(.Cell(ii, jj))
next jj
sql = sql & ")"
docmd.runsql sql
next ii
end with
docmd.setwarnings true
wrd.Quit
set wrddoc = nothing
set wrd = nothing
Related
I have never used Access VBA, but I need to create a module that parses a txt file and then immediately imports it into a table.
A dumbed-down of the txt is this:
15686541
468469
48978965
456287
48666545
45684651
456788
I need to parse it in order to
Remove all the line/rows that are not six characters long
Add commas after the third and fifth characters
The final result being something like:
468,46,9
456,28,7
456,78,8
All this must be done in an Access VBA module so that the importing process becomes seamless.
Thanks a lot!
Sorry to bother
This function will do that - and very fast:
Public Function ImportLog(ByVal Filename As String) As Long
Dim rs As DAO.Recordset
Dim File As Integer
Dim Data As String
Dim Data6 As String
Dim Records As Long
Set rs = CurrentDb.OpenRecordset("Select Top 1 * From YourTableName")
File = FreeFile()
Open Filename For Input As #File
While Not EOF(File)
Line Input #File, Data
If Len(Data) = 6 Then
Data6 = Space(6 + 2) ' Space for six digits and two commas.
' Build field value.
Mid(Data6, 1, 3) = Mid(Data, 1, 3)
Mid(Data6, 4, 1) = ","
Mid(Data6, 5, 2) = Mid(Data, 4, 2)
Mid(Data6, 7, 1) = ","
Mid(Data6, 8, 1) = Mid(Data, 6, 1)
rs.AddNew
' Adjust "Data" to your field name.
rs.Fields("Data").Value = Data6
rs.Update
Records = Records + 1
End If
Wend
Close #File
rs.Close
Set rs = Nothing
ImportLog = Records
End Function
The return value is the count of added records.
Try this:
Sub Import()
Dim fileNum As Integer
Dim dataLine As String
Dim column1 As String
Dim column2 As String
Dim column3 As String
fileNum = FreeFile()
Open "Filename.txt" For Input As #fileNum
While Not EOF(fileNum)
Line Input #fileNum, dataLine
If Len(dataLine) = 6 Then
column1 = Mid(dataLine, 1, 3)
column2 = Mid(dataLine, 4, 2)
column3 = Mid(dataLine, 6, 1)
CurrentDb.Execute "INSERT INTO YourTable(Column1, Column2, Column3) VALUES('" & column1 & "', '" & column2 & "', '" & column3 & "')"
End If
Wend
Close #fileNum
End Sub
I'm trying to show columns in a query where the value is not null in Ms Access (2010).
I have a query that now gives me this, where name, field1,... are the column headers:
Name | field1 | field2 | field 3 | field 4 | field5
mike | x | | x | x |
So now I don't want to see Field2 and Field5 in that query. I have a lot more fields then 5, and I only want to see the one with a value.
It's also fine If you can tell me a way to get the column names ( ex field1, field3,field4)
Thanks
You'll have to use a simple WHERE statement in your SQL. For example:
SELECT tbl.*, tbl.field2, tbl.field5
FROM tbl
WHERE tbl.field2 Is Not NULL AND tbl.field5 Is Not Null;
This will return all fields and all rows, where field2 and field5 are not Null.
EDIT: Since I am more familiar with vba, I would use the following approach to reach the desired output as stated in the comments.
Start by creating the query.
SELECT tbl.*
FROM tbl
WHERE tbl.ID = 12345;
Let's say you name it "qry_record". Afterwards, I would loop through the fields with a recorset, and create a new dynamic sql statement with the fields that have a value.
Dim rs As Recordset
Dim fld As Field
Dim sqlstatement As String: sqlstatement = "SELECT "
Set rs = CurrentDb.OpenRecordset("qry_record")
With rs
.MoveFirst
For Each fld In .Fields
If IsNull(fld.value) = False Then
Debug.Print fld.Name
sqlstatement = sqlstatement & "tbl." & fld.Name & ", "
End If
Next
End With
rs.Close
Set rs = Nothing
sqlstatement = sqlstatement & "FROM tbl"
DoCmd.RunSQL sqlstatement
EDIT: I have tested the code, and made a minor adjustment. Should work fine now on any table in your access database.
How can i compare two MS ACCESS 2007 databases.Both databases contain same tables with same feilds ad structure.i need to compare the record values between two databases to detect any difference in record values.
ACCESS 2007 Database1
serial no. | NAME | ADDRESS
1 smith street 1
2 john street 4
3 alix street 8
ACCESS 2007 Database2
serial no.| NAME | ADDRESS
1 smith street 1
2 jhn stret 4
3 alix street 8
I need a VBA code for ms access that can detect the differece of records,just as the records at serial number two.
First thing you should do is link in one of the tables to the other database, e.g link the Database 2 table into database one (this allows both to be queried together) then you could use this simple example with concatenation to determine if all the fields strung together match based on the serial number:
SELECT T1.*, T2.*
FROM Table1 As T1, Table2 As T2
WHERE T2.[serial no.] = T1.[serial no.]
AND T2.[NAME] & T2.[ADDRESS] <> T1.[NAME] & T1.[ADDRESS]
You could also specify the columns with each of their own condition if you prefer.
NOTE: This is assuming you are only looking for differences where the serial no matches, if you also need to identify records that may appear in one table but not the other then you will need to use an "Un-matched" query, the query designer can help you with this or post back and I can update my answer.
Option Compare Database
Private Sub Command4_Click()
Dim tablename1, tablename2 As String
tablename1 = Text0.Value
tablename2 = Text2.Value
'On Error GoTo Err_cmdValidateGeneralInfo_Click
Dim F As DAO.Field
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Set curDB = CurrentDb()
'If Me.DateModified = Date Then
'Adds new employees to the TT_GeneralInfo table in the FTEI_PhoneBook.mdb - which is used thru out the AP databases.
' DoCmd.OpenQuery "qryEmpData_TT_General"
strsql = "Select * from " & tablename1
Set rs = curDB.OpenRecordset(strsql)
strsql1 = "Select * from " & tablename2
DoCmd.CopyObject , "Unmatched_records", acTable, tablename1
curDB.Execute "DELETE FROM Unmatched_records"
Set rs1 = curDB.OpenRecordset(strsql1)
Do Until rs.EOF
For Each F In rs.Fields
If rs.Fields(F.Name) <> rs1.Fields(F.Name) Then
'rs.Edit
strsql = "Select * into test from " & tablename1 & " where " & F.Name & " = """ & rs.Fields(F.Name) & """"
DoCmd.RunSQL strsql
If DCount(F.Name, "test") <> 0 Then
GoTo append_unmatch
'appending unmacthed records
append_unmatch:
strsql2 = "insert into Unmatched_records Select * from test"
DoCmd.RunSQL strsql2
'if record doesnt match move to next one
GoTo Nextrecord
End If
' rs.Fields(F.Name) = rs1.Fields(F.Name)
' rs.Update
End If
Next F
Nextrecord:
rs.MoveNext
rs1.MoveNext
Loop
'To check whether tables matched or not
Dim rs2 As DAO.Recordset
strsql3 = "select * from Unmatched_records"
Set rs2 = curDB.OpenRecordset(strsql3)
For Each F In rs2.Fields
If DCount(F.Name, "Unmatched_records") <> 0 Then
MsgBox ("The two tables didnt match. Check table test for unmatching reocrds.")
Else
MsgBox ("Tables match!")
End If
Exit Sub
Next F
rs2.Close
End Sub
Function splitWords(ByVal strLine)
Dim ParsedStrLine() As String
ParsedStrLine = Split(strLine, ", ")
For i = LBound(ParsedStrLine) To UBound(ParsedStrLine)
splitWords = ParsedStrLine(i)
Next
End Function
I know next to nothing about VB.
I am trying to write a function that takes in a csv field and appends back to the same table in a different column.
THIS
someTable:
col_1 col_2
a x, y, z
TO THIS
someTable:
col_1 col_2
a x, y, z
x
y
z
Let me know if you need anymore information.
For just one row:
ParsedStrLine = Split(strLine, ", ")
For i = LBound(ParsedStrLine) To UBound(ParsedStrLine)
splitWords = ParsedStrLine(i)
CurrentDB.Execute "INSERT INTO tbl (Col1) VALUES ('" & splitWords & "'")
Next
But you may wish to work with the whole recordset.
Dim db As Database
Dim rs As Recordset
Dim rsAdd As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM Table1 WHERE Col_2 Is Not Null")
Set rsAdd = db.OpenRecordset("Table1")
Do While Not rs.EOF
ary = Split(rs!col_2, ", ")
For i = 0 To UBound(ary)
rsAdd.AddNew
rsAdd!col_1 = ary(i)
rsAdd.Update
Next
rs.MoveNext
Loop
In my database I have certain tables that have confidential information. Each of these tables contains an empty field called "ThisTableIsConfidential". I have a function that correctly displays a list of the tables that have this field in the Immediate window. Now I want to display the list on a form and I can't figure out how to do it. I thought to put the function in a query and display that in a listbox but the query didn't work. Any ideas?
This is the function (I cobbled it together from a few different online sources):
Function GetConfidentialTable()
Dim db As Database, tbl As TableDef, fld As Field, currentTable As String
Set db = CurrentDb
For Each tbl In db.TableDefs
If (tbl.Attributes = 0) Then
currentTable = tbl.Name
If FieldExists(currentTable, "ThisTableIsConfidential") = True Then
Debug.Print currentTable
End If
End If
Next tbl
End Function
You can set the listbox row source type to Value List and then use your function to return a list:
Function GetConfidentialTable()
Dim db As Database, tbl As TableDef, fld As Field, currentTable As String
Set db = CurrentDb
For Each tbl In db.TableDefs
If (tbl.Attributes = 0) Then
currentTable = tbl.Name
If FieldExists(currentTable, "ThisTableIsConfidential") = True Then
sList = sList & ";" & currentTable
End If
End If
Next tbl
GetConfidentialTable = Mid(sList,2)
End Function